选择多个标签 [英] Select with multiple tags
本文介绍了选择多个标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要修改以下查询,以便它要求当前元素上都存在两个标记.本质上要求tags_to_content
中存在2行,并具有特定ID ...
I'm wanting to modify the following query so that it requires both tags exist on the current element. It's essentially requiring that 2 rows exist in tags_to_content
with the specific ID...
SELECT
content.id,
content_text.content
FROM content
INNER JOIN tags_to_content ON (
tags_to_content2.tag_id IN (1, 2) AND
tags_to_content2.content_id = content.id
)
推荐答案
解决此问题的常用方法是为每个所需的标签加入:
The usual way to solve this is to join for each of the desired tags:
SELECT
content.id,
content_text.content
FROM content
INNER JOIN tags_to_content t1 ON
t1.content_id = content.id
INNER JOIN tags_to_content t2 ON
t2.content_id = content.id
WHERE t1.tag_id = 1
AND t2.tag_id = 2
这篇关于选择多个标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文