Select all projects that have matching tags

前端 未结 3 1038
粉色の甜心
粉色の甜心 2021-01-01 02:26

I\'m trying to find the most efficient way of dealing with this but I must tell you front-head I\'ve made a mess of it. Looked around SO and found nothing of relevance so he

3条回答
  •  再見小時候
    2021-01-01 02:33

    How about... (example for project 1)

    SELECT p.num, p.title
    FROM projects_to_tags pt1, projects_to_tags pt2, projects p
    where pt1.project_id = 1 and 
          pt2.project_id != 1 and 
          pt1.tag_id = pt2.tag_id and 
          p.num = pt2.project_id 
    group by pt2.project_id
    

    And maybe add a separate index for tag_id in projects_to_tags so you can use it alone, instead of the composite. No more type ALL. (Table Scan) Replacing both 1 with 4 give also the desired results.

提交回复
热议问题