在mysql中使用union,两边的sql语句可以进行order by排序吗?

如题所述

只能在语句最后使用order by,也就是对最后的结果排序追问

最后排序的话不行

追答select 字段 from 表1
union 
select 字段 from 表2
order by 字段

这种方式肯定是可以的

select 字段 from 表1
order by 字段
union 
select 字段 from 表2
order by 字段

这种写法肯定是错误的

温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2017-12-16
子查询一般是不建议orderby的 可以union之后在orderby
如果有要求需要子查询orderby的话
可以对着需要orderby的sql select top 一下
例如:
select top 1000 * from A orderby id

union
select top 1000 * from A orderby name追问

mysql中能使用top吗

追答

不好意思 原来用的忘了不能用top了
你用下limit看能不能打到这样的效果

本回答被提问者和网友采纳
第2个回答  2013-08-20
select * from 表名称 union select * from 表名称 order by 字段名称 DESC
前提是2个表的结构一模一样追问

这样不行我试了

相似回答