尝试限制count()不为0的结果时出现mysql错误#1064

时间:2011-12-28 16:48:36

标签: mysql mysql-error-1064

我有查询

SELECT helpc.cid, COUNT(help.cid) AS children_count FROM air_help_category AS helpc LEFT JOIN air_help AS help ON helpc.cid = help.cid GROUP BY cid WHERE hildren_count > 0

但我得到

  

#1064 - 您的SQL语法出错;检查与您的MySQL服务器版本对应的手册,以便在'WHERE children_count>附近使用正确的语法; 0限制0,30'在第1行

检查了相应的错误代码,但仍然无法解决此问题。

我所要做的就是找到那些至少有一个孩子的ids。

3 个答案:

答案 0 :(得分:2)

将'group'放在'group by'之前

答案 1 :(得分:0)

SELECT helpc.cid, COUNT(help.cid) AS children_count
FROM air_help_category AS helpc LEFT JOIN air_help AS help
ON (helpc.cid = help.cid) WHERE children_count < 0 GROUP BY cid

答案 2 :(得分:0)

首先,WHERE子句应该在GROUP BY之前。此外,您不能使用WHERE的汇总字段值 - 请改用HAVING,如下所示:

SELECT helpc.cid, COUNT(help.cid) AS children_count 
FROM air_help_category AS helpc LEFT JOIN air_help AS help ON helpc.cid = help.cid 
GROUP BY cid 
HAVING COUNT(help.cid) > 0