2011-05-31 183 views
1
SELECT jos_mod_gdwcomm.*, COUNT(jos_mod_gdwcomm_spam.reportid) `num_spam` 
FROM `jos_mod_gdwcomm` 
WHERE `num_spam` > 0 
LEFT JOIN `jos_mod_gdwcomm_spam` 
ON (jos_mod_gdwcomm.id = jos_mod_gdwcomm_spam.commid) 
GROUP BY jos_mod_gdwcomm.id 
ORDER BY `num_spam` DESC, jos_mod_gdwcomm_spam.datetime DESC 

我怎麼能修改此查詢,以便它只返回num_spam大於0MySQL查詢:使用LEFT JOIN

我tryed把

WHERE `num_spam` > 0 

在少數地方,但行沒有運氣。

感謝

回答

2

你需要一個HAVING子句:

SELECT jos_mod_gdwcomm.*, COUNT(jos_mod_gdwcomm_spam.reportid) `num_spam` 
FROM `jos_mod_gdwcomm` 
LEFT JOIN `jos_mod_gdwcomm_spam` 
ON (jos_mod_gdwcomm.id = jos_mod_gdwcomm_spam.commid) 
GROUP BY jos_mod_gdwcomm.id 

HAVING num_spam > 0 

ORDER BY `num_spam` DESC, jos_mod_gdwcomm_spam.datetime DESC 
2

嘗試用GROUP BY jos_mod_gdwcomm.id HAVING num_spam > 0

+0

感謝它工作不正常approuve你的答案在幾分鐘內不會讓我只是還沒有 – StiGMaT 2011-05-31 21:52:54

1

當您要測試聚合函數的條件時,可以使用HAVING子句。