简体   繁体   中英

SQL - Get difference of two sums

I am trying to get the difference of two sums using this:

SELECT ((SELECT SUM(`amount`)
         WHERE `reward_user_id` = 89
         FROM `plugin_reward_aggregated`)
        -
        (SELECT SUM(`amount`)
         WHERE `download_date` >= CURDATE ()
         FROM `plugin_reward_aggregated`));

but it shows an error:

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'WHERE reward_user_id = 89 FROM plugin_reward_aggregated ) ...' at line 2

You have SELECT SELECT in there, that's invalid syntax. It helps to format your query nicely instead of letting it just be one unreadably long line:

SELECT ((SELECT SUM(`amount`)
         WHERE `reward_user_id` = 89
         FROM `plugin_reward_aggregated`)
        -
        (SELECT SELECT SUM(`amount`)
         WHERE `download_date` >= CURDATE ()
         FROM `plugin_reward_aggregated`));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM