mysql:使用UNIONALL时,使用ORDERBY和limit进行单独的查询

inb24sb2  于 2021-06-25  发布在  Mysql
关注(0)|答案(1)|浏览(297)

我知道mysql语法不允许我们做我想做的事情,但我要求解决我的问题。
我想运行多个查询,并使用union通过使用orderby和limitpereach查询来显示完整的数据集。由于法律问题,我使用了假参数和表名。

select 
    x,
    y,
    z
from tbl1
where z = 'xxxx'
and y = 111
and x = 'text'
order by rand()
limit 11966
union all
select 
    x,
    y,
    z
from tbl1
where z = 'xxxx'
and y = 222
and x = 'text'
order by rand()
limit 3560
union all
select
.
.
.
.
.

有人知道解决方法吗?

bejyjqdl

bejyjqdl1#

使用括号:

(select ... order by ... limit ...) 
union all
(select ... order by ... limit ...) 
union all
(select ... order by ... limit ...) 
union all
...

相关问题