Skip to content

Instantly share code, notes, and snippets.

@pomek
Created April 4, 2015 16:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pomek/c7d899274def736175f4 to your computer and use it in GitHub Desktop.
Save pomek/c7d899274def736175f4 to your computer and use it in GitHub Desktop.
SELECT
DISTINCT w.name,
w.categoryId,
c.name as category
FROM
work w
JOIN
category c
ON
c.id = w.categoryId
INNER JOIN
(
SELECT
GROUP_CONCAT
(w.id ORDER BY w.id DESC) grouped
FROM
work w
GROUP BY
categoryId
ORDER BY
w.id ASC
) AS works
WHERE
FIND_IN_SET(w.id, works.grouped ) BETWEEN 1 AND ?
ORDER BY
categoryId ASC,
w.id DESC
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment