Skip to content

Instantly share code, notes, and snippets.

@leevigraham
Created October 11, 2011 09:43
Show Gist options
  • Save leevigraham/1277716 to your computer and use it in GitHub Desktop.
Save leevigraham/1277716 to your computer and use it in GitHub Desktop.
Select top 5 entries grouped by category
SELECT *
FROM
(
SELECT cp.cat_id, cp.entry_id, ct.title,
@num := if(@group = cp.cat_id, @num + 1, 1) as row_number,
@group := cp.cat_id as group_key
FROM (SELECT @group:=null,@num:=0) n
CROSS JOIN exp_category_posts cp
LEFT JOIN exp_channel_titles ct on cp.entry_id = ct.entry_id
ORDER BY cp.cat_id
) temp_table
WHERE row_number <= 5;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment