排除具有多对多关系的行 [英] Exclusion of rows with many-to-many relationships

查看:111
本文介绍了排除具有多对多关系的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个表:posts,tags和posts_has_tags(这促进了posts与tags之间的多对多关系).帖子可以具有任意数量的标签.

I have three tables: posts, tags and posts_has_tags (which facilitate the many-to-many relationship between posts and tags). A post can have any number of tags.

职位"表包含以下列: idposts 文字

The 'posts' table has the following columns: idposts text

标签"表包含以下内容: idtags 名称

The 'tags' table has these: idtags name

至于"posts_has_tags"表: posts_idposts tags_idtags

As for the 'posts_has_tags' table: posts_idposts tags_idtags

我不能做的是提出一个查询以选择所有帖子,除了那些在名称"列中为其分配了带有特定值的标签(或多个标签)的帖子之外.似乎其中应该包含"NOT EXISTS",但我无法真正将其完全包裹住.

What I can't do is come up with a query to select all posts except for those which have a tag (or tags) with a certain value in the 'name' column assigned to them. It seems that it should contain a "NOT EXISTS", but I can't really fully wrap my head around it.

谢谢您的帮助.

还可以同时将结果集限制为某些标签吗?例如:

Also, is it possible to restrict the result set to certain tags at the same time? For example:

要排除的标签:a,b 包含的标签:c

tags for exclusion: a, b tags for inclusion: c

发布带有标签'a','f'的帖子不会将其放入结果集中(因为如果包含标签,则不会添加到结果集中). 带有标签"a","b","c"的发布也不会进入结果集(因为排除了标签"a"和"b"). 带有标签"c","f"的发布确实将其放入结果集中,因为包含的标签是"c".

Post with tags 'a', 'f' doesn't make it into result set (because none if it's tags are included). Post with tags 'a', 'b', 'c' doesn't make it into result set either (because it's 'a' and 'b' are excluded tags). Post with tags 'c', 'f' does make it into result set, because 'c' is the included tag.

最终编辑 我终于找到了一个似乎可行且性能合理的解决方案:

FINAL EDIT I finally found a solution that seems to be working and reasonably well-performing: http://www.mysqldiary.com/a-many-to-many-relationship-table-solving-the-exclude-relation-problem/

推荐答案

我终于找到了一个似乎可行且性能合理的解决方案:

I finally found a solution that seems to be working and reasonably well-performing: http://www.mysqldiary.com/a-many-to-many-relationship-table-solving-the-exclude-relation-problem/

这篇关于排除具有多对多关系的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆