Skip to content

Instantly share code, notes, and snippets.

@utdrmac
Last active August 29, 2015 14:07
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 utdrmac/d125e72afe68e5331070 to your computer and use it in GitHub Desktop.
Save utdrmac/d125e72afe68e5331070 to your computer and use it in GitHub Desktop.
Solution to ORDER BY RAND()
-- Option 1
SELECT *
FROM title t
WHERE kind_id = 1 AND id >= FLOOR(1 + RAND() * (SELECT MAX(id) FROM title)) LIMIT 1;
-- Option 2
SELECT id, title
FROM title t RIGHT JOIN
(SELECT CEIL(RAND() * (SELECT MAX(id) FROM title WHERE kind_id = 1)) AS id) h USING (id);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment