0

I want to select 4 random rows from a table where level = 1 and 4 random rows from the same table where level=2. How can I do this in 1 query ?

4

2 回答 2

5
select * from (select * from your_table 
               where level = 1 order by rand() limit 4) x
union all
select * from (select * from your_table 
               where level = 2 order by rand() limit 4) y
于 2013-10-08T08:17:38.980 回答
0

try like this...

SELECT product_id, title, description FROM products WHERE active = 1 AND stock > 0 ORDER BY RAND() LIMIT 4;
于 2013-10-08T08:18:59.090 回答