0
我想從level = 1的表中選擇4個隨機行,並從level = 2的同一個表中選擇4個隨機行。我如何在1個查詢中做到這一點?在一個查詢中按不同條件選擇幾條隨機行
我想從level = 1的表中選擇4個隨機行,並從level = 2的同一個表中選擇4個隨機行。我如何在1個查詢中做到這一點?在一個查詢中按不同條件選擇幾條隨機行
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
嘗試像這樣...
SELECT product_id, title, description FROM products WHERE active = 1 AND stock > 0 ORDER BY RAND() LIMIT 4;