2013-11-04 76 views
-3

我想在mysql查詢中計算這兩列中的所有值。它不工作mysql sum()中的結果

SELECT sum(SELECT COUNT(even_id) 
      FROM all_tags 
      WHERE tag_id=h.tag_id) as posts , 
     sum(SELECT COUNT(even_id) 
      FROM all_tags 
      WHERE tag_id=h.tag_id and country_id=129) as local_posts 
from hashtags h 
where name like '%n%' 
+1

你不能簡單地粘貼您的查詢,而無需任何保養說明問題,目前情況和爲什麼「它不工作」(原文如此),並期望我們免費爲您調試它 –

+0

(顯然你可以) – Strawberry

回答

2

JOIN兩個表來代替,並使用CASE表達,這樣的事情:

SELECT 
    COUNT(a.even_id) as AllPosts , 
    SUM(CASE WHEN a.country_id = 129 THEN 1 ELSE 0 END) AS local_posts 
from hashtags h 
INNER JOIN all_tags AS a ON a.tag_id = h.tag_id 
where name like '%n%' 
+0

謝謝。非常感謝您的幫助 ! – user2950179