mysql交叉检查标签查询
我有两个表,一个包含项目,一个包含标签,我想找到项目表中具有标签“foo”但不包含标签栏的每个项目,所以如果我要选择具有标签“foo”的所有项目,我的查询将是
SELECT * FROM items i INNER JOIN tags t on i.id= t.id WHERE tag= "foo"
,我得到所有带有“foo”标签的项目,那么我如何子查询以取出所有也标记为“bar”的项目
编辑 基于下面提供的 Matthew Coxa 解决方案,我想出了这个,如果有人发现任何缺陷,请告诉我
SELECT * FROM items i INNER JOIN
tags t ON t.item_id=i.id
WHERE tag = 'foo'
AND NOT EXISTS (
SELECT * FROM tags tt WHERE t.item_id = tt.item_id AND tag = 'bar'
)
I have two tables one with Items and one with tags I want to find every item in the items table that has the tag "foo" but not the tag bar so if I where going to select all items that have the tag "foo" my query would be
SELECT * FROM items i INNER JOIN tags t on i.id= t.id WHERE tag= "foo"
and I get all items with tags of "foo" so how do I subquery to take out all the items that are also tagged "bar"
EDIT
based on Matthew Coxa solution provided bellow I cam up with this if anyone sees any flaw with this please let me know
SELECT * FROM items i INNER JOIN
tags t ON t.item_id=i.id
WHERE tag = 'foo'
AND NOT EXISTS (
SELECT * FROM tags tt WHERE t.item_id = tt.item_id AND tag = 'bar'
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
或者
or