如何联合使用Union和Order by
2012-08-03 17:48
435 查看
如果使用类似下面的SQL语句:
select columnA, columnB from tableA where columnA = 'Condition 1'
union
select columnC, columnD from tableB where columnC = 'Condition 1'
order by columnA asc
系统会报错:
消息 207,级别 16,状态 1,第 4 行
列名 'columnA' 无效。
消息 104,级别 16,状态 1,第 4 行
如果该语句包含 UNION、INTERSECT 或 EXCEPT 运算符,则 ORDER BY 项必须出现在选择列表中。
问题原因:
既然把多行给union了,把每列叫成什么名字都是不妥当的。。。只要采用列的序号即可。
select columnA, columnB from tableA where columnA = 'Condition 1'
union
select columnC, columnD from tableB where columnC = 'Condition 1'
order by 2 asc
这里需要注意的是,序号从1开始,如果指定的序号不在允许的范围内,系统会报出一个异常
select columnA, columnB from tableA where columnA = 'Condition 1'
union
select columnC, columnD from tableB where columnC = 'Condition 1'
order by columnA asc
系统会报错:
消息 207,级别 16,状态 1,第 4 行
列名 'columnA' 无效。
消息 104,级别 16,状态 1,第 4 行
如果该语句包含 UNION、INTERSECT 或 EXCEPT 运算符,则 ORDER BY 项必须出现在选择列表中。
问题原因:
既然把多行给union了,把每列叫成什么名字都是不妥当的。。。只要采用列的序号即可。
select columnA, columnB from tableA where columnA = 'Condition 1'
union
select columnC, columnD from tableB where columnC = 'Condition 1'
order by 2 asc
这里需要注意的是,序号从1开始,如果指定的序号不在允许的范围内,系统会报出一个异常
相关文章推荐
- 如何联合使用Union和Order by
- Mysql联合查询UNION和Order by同时使用报错问题的解决办法
- sql语句order by和union联合使用
- mybatis联合查询使用union,limit--order by不起作用
- union 或者 union all 与 order by 的联合使用
- Mysql联合查询UNION和Order by同时使用报错问题的解决办法
- mysql两个字段相同的表联合查询(union的使用)
- oracle使用order by排序null值如何处理
- 如何在C#中模拟C++的联合(Union)?[C#, C++] How To Simulate C++ Union In C#?
- Mysql联合查询UNION和UNION ALL的使用介绍
- Oracle中rownum和 order by联合使用导致排序错乱
- oracle使用order by排序null值如何处理
- 一个关于 联合查询union 和排序order by 的例子
- order by居然不能直接在union子句中使用
- 关于 联合查询 UNION 和Order by
- UNION中ORDER By的使用
- Mysql联合查询UNION和UNION ALL的使用介绍
- Hibernate如何实践union,order by,分页功能共存
- union联合的使用详解
- C++ 函数重载和union联合使用的威力