2012-03-26 105 views
1

我在编写查询时遇到了问题。我一直在UNION上处理两个查询,他们工作得很好。我尝试添加两个查询的结果时出现问题。在Mysql中添加两个不同查询的结果

这里有一些解释我自己。

//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. 

当我执行此查询时,出现此错误:

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 '+ 

我想我不应该用 “+” 号。有没有办法做到这一点? 非常感谢!

回答

4

你应该围绕整个查询的SELECT:

SELECT (SELECT COUNT(id) FROM table1) + (SELECT COUNT(id) FROM table2) AS count 
+0

谢谢,现在它的工作非常出色 – mauguerra 2012-03-26 20:17:05

1

尝试

SELECT (select count(id) from table1) + (select count(id) from table2) from dual; 
相关问题