您好,欢迎来到[编程问答]网站首页   源码下载   电子书籍   软件下载   专题
当前位置:首页 >> 编程问答 >> .NET >> union用法 如果语句中包含 UNION 运算符,那么 ORDER BY 子句中的项就必须出现在选择列表中。

union用法 如果语句中包含 UNION 运算符,那么 ORDER BY 子句中的项就必须出现在选择列表中。

来源:网络整理     时间:2016/7/23 22:49:02     关键词:union用法

关于网友提出的“union用法 如果语句中包含 UNION 运算符,那么 ORDER BY 子句中的项就必须出现在选择列表中。”问题疑问,本网通过在网上对“union用法 如果语句中包含 UNION 运算符,那么 ORDER BY 子句中的项就必须出现在选择列表中。”有关的相关答案进行了整理,供用户进行参考,详细问题解答如下:

问题:union用法 如果语句中包含 UNION 运算符,那么 ORDER BY 子句中的项就必须出现在选择列表中。
描述:

select top 10 * from bolemobile where Jobname='会计' and  bestflag ='1' 
union 
select * from bolemobile where Jobname='会计' and bestflag not in (select bestflag from bolemobile where bestflag ='1')
order by bestflag desc, lastupdate_time desc
用*不出错(上),但是单指定某几列就出错了“如果语句中包含 UNION 运算符,那么 ORDER BY 子句中的项就必须出现在选择列表中。”


解决方案1:

学习

解决方案2:

指定的那几列必须包含bestflag, lastupdate_time               
over

解决方案3:

select * from 
(
select top 10 * from bolemobile where Jobname='会计' and  bestflag ='1'
union
select * from bolemobile where Jobname='会计' and bestflag not in (select bestflag from bolemobile where bestflag ='1') 
)
order by bestflag desc, lastupdate_time desc 

解决方案4:

引用 3 楼 lovehongyun 的回复:
错误报的很清楚了

用来 order  的字段需要出现在 select 中
解决方案5:

引用 3 楼 lovehongyun 的回复:
错误报的很清楚了.

就是啊,你要是单指定某几列的话,那么order by也得出现这几列 解决方案6:

举个例子

select * from table1 where id in (13,14)
union
select * from table1 where id in (12,22)
order by id desc

上面是对的
select Department from table1 where id in (13,14)
union
select Department from table1 where id in (12,22)
order by id desc

上面是错的
ORDER BY items must appear in the select list if the statement contains a UNION operator.
select Department,id from table1 where id in (13,14)
union
select Department,id from table1 where id in (12,22)
order by id desc

上面也是对的 解决方案7:

也可以用视图


Create view MyView as 
select top 10 * from bolemobile where Jobname='会计' and  bestflag ='1'
union
select * from bolemobile where Jobname='会计' and bestflag not in (select bestflag from bolemobile where bestflag ='1') 
go
select * from myview  order by bestflag desc, lastupdate_time desc 
解决方案8:

错误报的很清楚了.

解决方案9:


select * from 
(
select top 10 * from bolemobile where Jobname='会计' and  bestflag ='1'
union
select * from bolemobile where Jobname='会计' and bestflag not in (select bestflag from bolemobile where bestflag ='1') 
)
order by bestflag desc, lastupdate_time desc 
解决方案10:

排序的列 必须出现在 你筛选的列中


以上介绍了“union用法 如果语句中包含 UNION 运算符,那么 ORDER BY 子句中的项就必须出现在选择列表中。”的问题解答,希望对有需要的网友有所帮助。
本文网址链接:http://www.codes51.com/itwd/2871862.html

union用法相关图片

union用法相关文章