在一个查询中按不同标准选择几个随机行

Lac*_*hev 0 mysql random

我想从level = 1的表中选择4个随机行,并从同一个表中选择4个随机行,其中level = 2.我如何在1个查询中执行此操作?

jue*_*n d 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
Run Code Online (Sandbox Code Playgroud)