I have a MySQL table with a column called "priority". The column can have two values: high or low. I want to select 8 records from the table at random, but I want 6 of them to be high priority, and 2 of them to be low priority. If possibly, I would like to do it in one SQL statement. Is there any way to do two LIMITS in one query based on this kind of criteria?
views:
61answers:
4
A:
Use a union, so you have two select statements in one, basically.
James Black
2009-10-21 16:03:11
+7
A:
SELECT t1.*
FROM table t1
WHERE priority = 'high'
ORDER BY rand() Limit 8
UNION ALL
SELECT t1.*
FROM table t1
WHERE priority = 'low'
ORDER BY rand() Limit 2
najmeddine
2009-10-21 16:03:45
Thank you very much.
andyashton
2009-10-21 16:21:47
you're welcome.
najmeddine
2009-10-21 16:32:34
A:
Use a union:
SELECT * FROM table WHERE priority = 'high' ORDER BY RAND() LIMIT 8
UNION ALL
SELECT * FROM table WHERE priority = 'low' ORDER BY RAND() LIMIT 2
jamuraa
2009-10-21 16:05:28