1

我收到错误消息

您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在 GROUP BY name HAVING COUNT(DISTINCT goodfor_when.name) = 1 附近使用正确的语法。

这是发生错误的查询的一部分

                    ....
        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
4

1 回答 1

4

同一查询中不能有两个HAVING或两个命令,并且必须放在. 因此,例如:GROUP BYGROUP BYHAVING

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并且GROUP BY t.id如果你想ORDER BY t.id

于 2012-11-29T14:04:32.853 回答