Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 使用WHERE IN和HAVING COUNT的SELECT语句失败_Mysql_Where_Having_Where In - Fatal编程技术网

Mysql 使用WHERE IN和HAVING COUNT的SELECT语句失败

Mysql 使用WHERE IN和HAVING COUNT的SELECT语句失败,mysql,where,having,where-in,Mysql,Where,Having,Where In,我使用以下查询返回所有用户属性和具有指定标记的属性: SELECT `user_attributes`.*, `attributes`.* FROM `user_attributes` INNER JOIN `attributes` ON (`attributes`.`id` = `user_attributes`.`attribute_id`) INNER JOIN `user_tags` ON (`attributes`.`id` = `user_tags`.`attribute_id`)

我使用以下查询返回所有用户属性和具有指定标记的属性:

SELECT `user_attributes`.*, `attributes`.*
FROM `user_attributes`
INNER JOIN `attributes` ON (`attributes`.`id` = `user_attributes`.`attribute_id`)
INNER JOIN `user_tags` ON (`attributes`.`id` = `user_tags`.`attribute_id`)
INNER JOIN `tags` ON (`user_tags`.`tag_id` = `tags`.`id`)
WHERE `user_attributes`.`user_id` = '1'
    AND `tags`.`title` IN ('tag1')
我想调整查询,以便它找到所有有2个标记的值。目前我有:

SELECT `user_attributes`.*
FROM `user_attributes`
INNER JOIN `attributes` ON (`attributes`.`id` = `user_attributes`.`attribute_id`)
INNER JOIN `user_tags` ON (`attributes`.`id` = `user_tags`.`attribute_id`)
INNER JOIN `tags` ON (`user_tags`.`tag_id` = `tags`.`id`)
WHERE `user_attributes`.`user_id` = '1'
    AND `tags`.`title` IN ('tag1', 'tag2')
    HAVING (COUNT(DISTINCT `tags`.`title`) = 2)

是因为我在使用“没有分组依据的拥有”吗?

确实应该将“拥有”与“分组依据”结合使用。MySQL是唯一一个没有分组的数据库

还有一些证据可以证明选民的不满。

MySQL http://www.sqlfiddle.com/#!2/ba8d6/3  (this is WRONG and looks like HAVING IS USED AS WHERE) 
Oracle http://www.sqlfiddle.com/#!4/ba8d6/1 (this is correct ORA-00979: not a GROUP BY expression Oracle is missing the GROUP BY)
Postgresql http://www.sqlfiddle.com/#!1/ba8d6/2 (this is correct ERROR: column "data.username" must appear in the GROUP BY clause or be used in an aggregate function   Postgresql wants to have an GROUP BY

have应该与groupby结合使用。MySQL是唯一一个没有分组的数据库,谢谢,如果你把它作为一个答案,我会接受的。我一直把小组放在错误的地方,并得到一个语法错误。需要在have之前和WHERE and之后。您可能需要
按用户\u属性分组。在这个特定查询中,属性\u id
(因为您说要返回所有用户\u属性…),但是
have
而不
groupby
在其他情况下是有效和有用的。