Reputation: 3858
I am having trouble writing a query. I have been working with UNION on two queries and they work just fine. My problem comes when I try to add the result of two queries.
Here is something to explain myself.
//Query 1
select count(id) from table1 <-- This gives a result of 2
//Query 2
select count(id) from table2 <-- This gives a result of 1
//What I want to do is to add the two queries (2 + 1 = 3):
(select count(id) from table1) + (select count(id) from table2) <-- Which gives a result of 3.
When I execute this query, this error appears:
ERROR 1064 (42000): 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 '+
I think I should not use the "+" sign. Is there any way to do this? Thank you a lot!
Upvotes: 3
Views: 6233
Reputation: 5101
Try
SELECT (select count(id) from table1) + (select count(id) from table2) from dual;
Upvotes: 1
Reputation: 2056
You should have a SELECT around the whole query:
SELECT (SELECT COUNT(id) FROM table1) + (SELECT COUNT(id) FROM table2) AS count
Upvotes: 8