gpt4 book ai didi

mysql - 在单个查询中垂直堆叠 MySQL 结果

转载 作者:可可西里 更新时间:2023-11-01 06:43:10 24 4
gpt4 key购买 nike

我如何将以下 3 个查询合并(垂直堆叠)到 一个查询 中,该查询返回 100 行,50 行来自类别 1,25 行来自类别 2,25 行来自类别 3,所有这些都是随机选择的。我尝试了 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;

最佳答案

要将 ORDER BYLIMIT 应用于单个 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);

关于mysql - 在单个查询中垂直堆叠 MySQL 结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9983137/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com