Skip to content

Instantly share code, notes, and snippets.

@eet-nu
Created April 15, 2010 14:32
Show Gist options
  • Save eet-nu/367147 to your computer and use it in GitHub Desktop.
Save eet-nu/367147 to your computer and use it in GitHub Desktop.
SELECT tags.*, taggings.count
FROM tags
JOIN (
SELECT
taggings.tag_id,
count(*) as count
FROM taggings
INNER JOIN companies ON companies.id = taggings.taggable_id
AND taggings.taggable_type = 'Company'
AND `companies`.`visible` = 1
AND `companies`.`closed` = 0
AND `companies`.`registration` = 0
JOIN taggings companies_taggings_dutch_230
ON companies_taggings_dutch_230.taggable_id = companies.id
AND companies_taggings_dutch_230.taggable_type = 'Company'
AND companies_taggings_dutch_230.tag_id = 47
GROUP BY taggings.tag_id
HAVING COUNT(*) > 0
) AS taggings ON taggings.tag_id = tags.id
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment