SQL查询查找包含多个标签的资源
我有两张桌子。
Resource:
md5 (PK), link, title
Tags:
md5 (FK), category
它是一对多的关系,因此一个资源可以有多个标签。 我希望能够提取具有两个标签的资源,例如包含标签“web”和“blog”的资源。如果我使用“OR”,它显然会返回仅包含“web”或仅包含“blog”的文档,但如果我使用AND,即使我知道有包含这两个标签的资源,我也不会得到任何结果
SELECT DISTINCT tags.MD5, Resource.Title, Resource.Link, tags.Category
FROM Resource
INNER JOIN tags ON Resource.MD5 = tags.MD5
WHERE
(tags.Category = @tag)
OR
(tags.Category = @tag2)
ORDER BY tags.MD5
I have two tables.
Resource:
md5 (PK), link, title
Tags:
md5 (FK), category
it is a one to many relationship such that a resource can have a number of tags.
I want to be able to extract a resource that has both tags, for e.g. a resource that contains the tag 'web' and 'blog' in it. If I use 'OR', it will obviously return even document that contain only 'web' or only contain 'blog', but if I use AND, I get no results even though I know there are resources that contain both tags
SELECT DISTINCT tags.MD5, Resource.Title, Resource.Link, tags.Category
FROM Resource
INNER JOIN tags ON Resource.MD5 = tags.MD5
WHERE
(tags.Category = @tag)
OR
(tags.Category = @tag2)
ORDER BY tags.MD5
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你想要这样做:
You want to do like that:
以下将过滤掉不具有这两个标签的资源:
The following will filter out resources that don't possess both tags: