我有以下查询来返回所有具有指定标签的 user_attributes 和属性:
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)
是否因为我在没有 GROUP BY 的情况下使用 HAVING 而中断?