我如何将以下3个查询组合(垂直堆叠)到一个查询中,该查询返回100行,类别1中的50行,来自类别2的25个,来自类别3的25个,都是随机选择的.我试过UNION但似乎没有用.
select * from table where category_id = 1 order by rand() limit 50;
select * from table where category_id = 2 order by rand() limit 25;
select * from table where category_id = 3 order by rand() limit 25;
Run Code Online (Sandbox Code Playgroud)
要应用ORDER BY或LIMIT个人SELECT,请将子句放在括起来的括号内SELECT:
(select * from table where category_id = 1 order by rand() limit 50)
UNION ALL
(select * from table where category_id = 2 order by rand() limit 25)
UNION ALL
(select * from table where category_id = 3 order by rand() limit 25);
Run Code Online (Sandbox Code Playgroud)