Skip to content

Instantly share code, notes, and snippets.

@objectivehtml
Created March 29, 2012 14:17
Show Gist options
  • Save objectivehtml/2237902 to your computer and use it in GitHub Desktop.
Save objectivehtml/2237902 to your computer and use it in GitHub Desktop.
Return channel entries with all the categories grouped in a single category
SELECT GROUP_CONCAT(CONVERT(CONCAT(',', `exp_category_posts`.`cat_id`), CHAR(8)) SEPARATOR ' ') as `category_ids`
FROM `exp_channel_data`
LEFT JOIN `exp_category_posts` ON `exp_channel_data`.`entry_id` = `exp_category_posts`.`entry_id`
LEFT JOIN `exp_channel_titles` ON `exp_channel_data`.`entry_id` = `exp_channel_titles`.`entry_id`
GROUP BY `exp_channel_data`.`entry_id`
HAVING `category_ids` LIKE '%,10 %' AND `category_ids` LIKE '%,1 %'
LIMIT 0 , 30
@adrienne
Copy link

SELECT ct.title, cd.* FROM
    (
    SELECT distinct entry_id, COUNT(cat_id) AS ct
    FROM exp_category_posts
    WHERE
    # OR all your categories together here 
        cat_id = 19
        OR cat_id = 30
    GROUP BY entry_id
    # this should be the TOTAL number of categories you're searching on
    HAVING ct = 2
    ) cc
INNER JOIN
    exp_channel_data cd
    USING (entry_id)
INNER JOIN exp_channel_titles ct
    USING (entry_id)
# you can put other conditions here
WHERE field_id_2 = 'something'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment