mysql: finding rows that have multiple tags and the same id

后端 未结 1 1306
长情又很酷
长情又很酷 2021-02-10 15:18

I\'m having an issue figuring the mysql to find links that have two specific \'tags\' and the same \'hashid\' when doing a JOIN of two tables

Assume my tables look like

1条回答
  •  [愿得一人]
    2021-02-10 15:41

    The type of problem is called Relational Division

    SELECT  a.md5, 
            a.url,
            a.title
    FROM    Links a
            INNER JOIN Tags b
                ON a.md5 = b.md5
    WHERE   b.Tag IN ('awesome', 'useful') -- <<== list of desired tags
    GROUP   BY a.md5, a.url, a.title
    HAVING  COUNT(*) = 2                   -- <<== number of tags defined
    
    • SQLFiddle Demo
    • SQL of Relational Division

    OUTPUT

    ╔══════╦════════════╦═══════╗
    ║ MD5  ║    URL     ║ TITLE ║
    ╠══════╬════════════╬═══════╣
    ║ a0a0 ║ google.com ║ foo   ║
    ╚══════╩════════════╩═══════╝
    

    0 讨论(0)
提交回复
热议问题