I got error message
You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near GROUP BY name HAVING COUNT(DISTINCT goodfor_when.name) = 1 .
Here is part of the query where error happen
....
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`;
But works if remove line
HAVING COUNT(DISTINCT `goodfor_what`.`name`) = 3 AND `goodfor_when`.`name` IN ('Daytime') GROUP BY name
You can’t have two
HAVINGor twoGROUP BYcommands in the same query, and theGROUP BYmust be placed before theHAVING. So, for instance :And more, unless
t.idis an aggregated column, you’ll have toSELECT t.idandGROUP BY t.idif you want toORDER BY t.id