Skip to content

Instantly share code, notes, and snippets.

@utdrmac
Last active August 29, 2015 14:07
Embed
What would you like to do?
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