2012-03-26 49 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; 
相關問題