V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
alayii
V2EX  ›  问与答

Mysql 查找包含多个标签的文章?

  •  
  •   alayii · Jan 21, 2015 · 4452 views
    This topic created in 4128 days ago, the information mentioned may be changed or developed.

    文章包含多个标签,用关联表外理,表 articles, re_articles_tags, tags

    如何能查找即有 A 标签又有 B 标签的文章?

    4 replies    2015-01-21 15:00:23 +08:00
    cevincheung
        1
    cevincheung  
       Jan 21, 2015   ❤️ 1
    select * from articles where article_id in (select distinct(article_id) from tags where tags in ('tag1','tag2','tag3')) limit n
    caixiexin
        2
    caixiexin  
       Jan 21, 2015   ❤️ 1
    select distinct a.* from articles a, re_articles_tags b where a.文章id=b.文章id and b.tag in (A ,B)
    ?
    feiyuanqiu
        3
    feiyuanqiu  
       Jan 21, 2015
    一个能用但是不太好的 SQL:

    SELECT
    DISTINCT a.*
    FROM
    re_articles_tags r1
    JOIN re_articles_tags r2
    ON r1.`article_id` = r2.`article_id`
    AND r1.`tag_id` = 1
    AND r2.`tag_id` = 2
    JOIN articles a
    ON r1.`article_id` = a.`article_id`
    pi1ot
        4
    pi1ot  
       Jan 21, 2015
    这应该是适合lucene做的事情
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3459 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 68ms · UTC 11:19 · PVG 19:19 · LAX 04:19 · JFK 07:19
    ♥ Do have faith in what you're doing.