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.   

    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 
      

  2.   

    也可以用视图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') goselect * from myview  order by bestflag desc, lastupdate_time desc 
      

  3.   

    举个例子
    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
    上面也是对的
      

  4.   

    就是啊,你要是单指定某几列的话,那么order by也得出现这几列
      

  5.   


    用来 order  的字段需要出现在 select 中
      

  6.   

    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 
    这样是可以的 ,但是吧*改成某几列就不行了 
      

  7.   

    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 
      

  8.   

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

  9.   

    意思是排序的列 必须出现在 你筛选的列中.
    比如想选择col1,col2,col3,然后以bestflag desc, lastupdate_time 排序
    则,
    select col1,col2,col3
    FROM
    (
    select top 10 col1,col2,col3,bestflag , lastupdate_time  from bolemobile where Jobname='会计' and  bestflag ='1' 
    union 
    select col1,col2,col3,bestflag , lastupdate_time   from bolemobile where Jobname='会计' and bestflag not in (select bestflag from bolemobile where bestflag ='1') 
     ) 
    order by bestflag desc, lastupdate_time desc