sql查询的问题由groupby计数

时间:2015-06-08 03:43:58

标签: mysql sql

我有以下查询:

SELECT s.username FROM `instagram_shop` s
INNER JOIN `instagram_shop_picture` p
ON s.id = p.shop_id
WHERE s.`deletedAt` IS NULL
HAVING COUNT(p.id) = 0
GROUP BY s.id

我一直收到以下错误:

  

您的SQL语法有错误;检查手册   对应于您的MySQL服务器版本,以便使用正确的语法   在'GROUP BY s.id LIMIT 0,30'附近的第6行

附近

这里有什么问题?

2 个答案:

答案 0 :(得分:1)

HAVING子句之后移动GROUP BY子句。

MySQL非常关注SELECT语句中关键字/子句的顺序

参考:13.2.9 SELECT Syntax https://dev.mysql.com/doc/refman/5.6/en/select.html

答案 1 :(得分:0)

Try the below one :
there is an syntax issue with your query, 
Having should follow after GROUP BY caluse.

Also the columns which you mention in your GROUP BY 
clause should be present in the SELECT list.

SELECT s.id,s.username FROM `instagram_shop` s
INNER JOIN `instagram_shop_picture` p
ON s.id = p.shop_id
WHERE s.deletedAt IS NULL 
GROUP BY s.id,s.username 
HAVING COUNT(s.id) = 0