mysql 5.7.15 union order by 子查询排序不生效
生活随笔
收集整理的這篇文章主要介紹了
mysql 5.7.15 union order by 子查询排序不生效
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
其實這么寫理論上是對的,很多博客教程都是這樣子寫的。。
select * from (SELECT * FROM article WHERE is_top=0 ORDER BY id DESC) as t1unionselect * 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 t1unionselect * 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 t1unionselect * from (SELECT * FROM article WHERE is_top=1 ORDER BY start_time desc limit 999999) as t2
然后還是發現子查詢的排序無效,尷尬,后來才發現如果order by 不帶limit,會被優化器干掉,導致語句就是: select * from (SELECT * FROM article WHERE is_top=0 ) as t1unionselect * 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 t1unionselect * from (SELECT * FROM article WHERE is_top=1 ORDER BY start_time desc limit 999999) as t2
總結
以上是生活随笔為你收集整理的mysql 5.7.15 union order by 子查询排序不生效的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 马斯克称特斯拉是全世界最安全的车 在刹车
- 下一篇: 在将规则文件上传到sonarQube时,