Need help with sql query to find things tagged with all specified tags

后端 未结 2 1664
予麋鹿
予麋鹿 2020-12-08 17:44

Let\'s say I have the following tables:

TAGS

id: integer
name: string

POSTS

id: integer
body: text

TAGGINGS

id:

2条回答
  •  执笔经年
    2020-12-08 18:18

    Try this:

    Select * From Posts p
       Where Not Exists
           (Select * From tags t
            Where name in 
               ('Cheese', 'Wine', 'Paris', 
                 'Frace', 'City', 'Scenic', 'Art')
               And Not Exists
                 (Select * From taggings
                  Where tag_id = t.Tag_Id
                    And post_Id = p.Post_Id))
    

    Explanation: Asking for a list of those Posts that have had every one of a specified set of tags associated with it is equivilent to asking for those posts where there is no tag in that same specified set, that has not been associated with it. i.e., the sql above.

提交回复
热议问题