MySQL select join where AND where

后端 未结 3 1132
忘了有多久
忘了有多久 2020-12-14 21:10

I have two tables in my database:

Products

  • id (int, primary key)
  • name (varchar)

ProductTags

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-14 21:49

    you need to have a group by / count to ensure all are accounted for

    select Products.*
      from Products 
             join ( SELECT Product_ID
                      FROM ProductTags
                      where ProductTags.tag_id IN (1,2,3)
                      GROUP BY Products.id
                      having count( distinct tag_id ) = 3 ) PreQuery
            on ON Products.id = PreQuery.product_id 
    

提交回复
热议问题