I have two tables in a database, communities
and daily_shares
I would like to make the difference of the sum of the first one with the second one:
INSERT INTO
shares_diff (`date`, `shares_copylink`, `shares_email`, `shares_facebook`, `shares_messenger`,
`shares_pinterest`, `shares_twitter`, `shares_whatsapp`)
SELECT CURDATE() AS date,
SUM(shares_copylink) FROM communities - SUM(shares_copylink) FROM daily_shares,
SUM(shares_email) FROM communities - SUM(shares_copylink) FROM daily_shares,
SUM(shares_facebook) FROM communities - SUM(shares_copylink) FROM daily_shares,
SUM(shares_messenger) FROM communities - SUM(shares_copylink) FROM daily_shares,
SUM(shares_pinterest) FROM communities - SUM(shares_copylink) FROM daily_shares,
SUM(shares_twitter) FROM communities - SUM(shares_copylink) FROM daily_shares,
SUM(shares_whatsapp) FROM communities - SUM(shares_copylink) FROM daily_shares
WHERE EXISTS (SELECT * FROM daily_shares);
But it returns me:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '- SUM(shares_copylink) FROM daily_shares,
SUM(shares_email) FROM communi' at line 5
Here is the schema .
I use MySQL 5.7
The detail of the syntax is because you integrate multiple queries but it is not specified where one ends and another starts. This is solved by adding some parentheses in your queries, leaving your result as follows: