您的位置:首页 > 其它

union与union all与order by

2014-04-29 17:42 411 查看
有时候我们会将两部分排序的结果联合起来显示,union会自动排序和去重,union all则不会

看以下sql语句:

select a,b,c from AA aa where ...... order by aa.a desc

union all

select a,b,c from BB bb where ...... order by bb.a desc

会报错,原因是union all 最后一个select才能orderby 或者就嵌套一层再order by,如下:

select * from (select a,b,c from AA aa where ...... order by aa.a desc)

union all

select * from (select a,b,c from BB bb where ...... order by bb.a desc)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: