我收到错误信息
SQL语法中有错误;请查看手册
对应于MySQL服务器版本,以便使用正确的语法
当.name=1时,按具有COUNT(DISTINCT goodfor_)的名称分组。
以下是发生错误的部分查询

                    ....
        WHERE
        ( `city`.`name` = "London" ) AND
        `goodfor_what`.`name` IN ("Beers","Wine","Food")
        GROUP BY name

        HAVING COUNT(DISTINCT `goodfor_what`.`name`) = 3 AND `goodfor_when`.`name` IN ('Daytime') GROUP BY name
        HAVING COUNT(DISTINCT `goodfor_when`.`name`) = 1 ORDER BY `t`.`name`, `t`.`id`;

但如果移除线路
HAVING COUNT(DISTINCT `goodfor_what`.`name`) = 3 AND `goodfor_when`.`name` IN ('Daytime') GROUP BY name

最佳答案

在同一个查询中不能有两个HAVING或两个GROUP BY命令,GROUP BY必须放在HAVING之前。例如:

GROUP BY name
HAVING (COUNT(DISTINCT `goodfor_what`.`name`) = 3 AND `goodfor_when`.`name` IN ('Daytime'))
OR COUNT(DISTINCT `goodfor_when`.`name`) = 1
ORDER BY `t`.`name`, `t`.`id`;

而且,除非t.id是聚合列,否则如果要SELECT t.id

关于mysql - HAVING COUNT(DISTINCT`goodfor_what`.`name`)= 3导致错误消息,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13627446/

10-17 00:01
查看更多