此代码无法正常工作。我想选择所有带有TAGS 561, 562的帖子,一起或单独选择,但没有此处没有的任何标签->>> IN (561, 562)

 SELECT post_id_post
 FROM post_has_tags
 WHERE tags_id_tags IN (561, 562)
 HAVING COUNT(tags_id_tags) <= 2

post_id_post        tags_id_tags
600                    561
600                    562
600                    917  // 917 is not inside IN (561, 562)


但是此代码将输出post600。这是错误的,正确的输出应该没有结果。

谢谢

最佳答案

尝试这个:

select distinct p1.post_id_post from post_has_tags p1
where not exists (
  select * from post_has_tags p2
  where p1.post_id_post = p2.post_id_post and p2.tags_id_tags not in (561, 562)
)


我不想比菲尔少,所以我也添加了fiddle :)

关于mysql - 得到错误的选择,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9917497/

10-13 02:02