开发者

Select with multiple tags

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜