mysql union 和 order by

使用了union关联两个表 但是无做到排序 令我 很慌啊!

一、关联查询 

select order_no from tb1 where uid=1 union select order_no from tb2 where uid= 2

二、需求变动  需要 按照  create_time 排序 又不想分开写了

(select order_no from tb1 where uid=1   ORDER BY create_time desc) union (select order_no from tb2 where uid=2  ORDER BY create_time desc)

按照这种写法不可以用的话 就只能往下看了

三、如果没有limit order by好像就被屏蔽了 这样写就可以了

(select order_no from tb1 where uid=1   ORDER BY create_time desc limit 22 ) union (select order_no from tb2 where uid=2  ORDER BY create_time desc limit 22)
发布了52 篇原创文章 · 获赞 11 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_40816144/article/details/103699861