mysql 5.7.15 union order by 子查询排序不生效

其实这么写理论上是对的,很多博客教程都是这样子写的。。
select * from (SELECT * FROM article WHERE is_top=0 ORDER BY id DESC) as t1
union
select * from (SELECT * FROM article WHERE is_top=1 ORDER BY start_time desc) as t2

然后还是发现子查询的排序无效,尴尬,后来才发现如果order by 不带limit,会被优化器干掉,导致语句就是:
select * from (SELECT * FROM article WHERE is_top=0 ) as t1
union
select * from (SELECT * FROM article WHERE is_top=1) as t2

解决方案:
select * from (SELECT * FROM article WHERE is_top=0 ORDER BY id DESC limit 999999) as t1
union

select * from (SELECT * FROM article WHERE is_top=1 ORDER BY start_time desc limit 999999) as t2


重点是limit!!!!!

转自:

http://blog.csdn.net/emaste_r/article/details/73550783


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值