Finding posts with tag1 AND tag2? (using a join-table) Exists / Having / subqueries… What to use?

前端 未结 2 848
自闭症患者
自闭症患者 2021-01-28 05:08

I need to make a query to search and filter on multiple terms.

I have a table with weapons and all can have multiple tags. I want to be able to create a filter o

2条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-28 05:23

    Try this:

    SELECT * FROM wp_posts AS p
    LEFT JOIN wp_term_relationships AS tr ON p.ID = tr.object_id 
    LEFT JOIN wp_terms AS t ON tr.term_taxonomy_id = t.term_id 
    WHERE p.id IN 
    (
        SELECT p2.id FROM wp_posts AS p2
        LEFT JOIN wp_term_relationships AS tr2 ON p2.ID = tr2.object_id 
        LEFT JOIN wp_terms AS t2 ON tr2.term_taxonomy_id = t2.term_id 
        GROUP BY p2.id
        HAVING FIND_IN_SET('blue', GROUP_CONCAT(t2.term)) AND FIND_IN_SET('old', GROUP_CONCAT(t2.term))
    )
    

提交回复
热议问题