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大于0的行。
我试着把
WHERE `num_spam` > 0
在一些地方,但没有运气。
由于
答案 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
答案 1 :(得分:2)
尝试使用GROUP BY jos_mod_gdwcomm.id HAVING num_spam > 0
。
答案 2 :(得分:1)
如果要在聚合函数上测试条件,请使用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 COUNT(jos_mod_gdwcomm_spam.reportid) > 0
ORDER BY `num_spam` DESC, jos_mod_gdwcomm_spam.datetime DESC