select * from 表 where 字段A like '1%' and 字段B<>1
select * from 表 where 字段A like 'X%'
select * from 表 where 字段A like 'M%' or 字段B=1

解决方案 »

  1.   

    是不是这样select 1 as 次序,* from 表 where 字段A like '1%' and 字段B<>1
    union
    select 2 as 次序,* from 表 where 字段A like 'X%'
    union
    select 3 as 次序,* from 表 where 字段A like 'M%' or 字段B=1
      

  2.   

    select * from 表 where 字段A like '1%' and 字段B<>1
    union all
    select * from 表 where 字段A like 'X%'
    union all
    select * from 表 where 字段A like 'M%' or 字段B=1
      

  3.   

    不好意思,又添新问题了。
    1、2、3中分别都要以字段C升序排列。
    我试过用order by,但报错,是不是用了union就不能用order by了。
    如何解决排序的问题啊?
      

  4.   

    select * from 表 into #temp order by 字段Cselect * from #temp where 字段A like '1%' and 字段B<>1
    union all
    select * from #temp where 字段A like 'X%'
    union all
    select * from #temp where 字段A like 'M%' or 字段B=1
      

  5.   

    如果要对union操作的数据集合结果进行排序,必须把order by子句写在最后的select子句后面,但是进行排序的依据必须是第一个select列表中的列,否则系统会提示出错
      

  6.   

    不同表:
    select * from (select * from 表1 order by 字段C)A where 字段A like '1%' and 字段B<>1
    union all
    select * from (select * from 表2 order by 字段C)A where 字段A like 'X%'
    union all
    select * from (select * from 表3 order by 字段C)A  where 字段A like 'M%' or 字段B=1
      

  7.   

    select * from (
    select * from 表 where 字段A like '1%' and 字段B<>1
    union all
    select * from 表 where 字段A like 'X%'
    union all
    select * from 表 where 字段A like 'M%' or 字段B=1 )order by ...
      

  8.   

    select * from (
    select * from 表 where 字段A like '1%' and 字段B<>1
    union all
    select * from 表 where 字段A like 'X%'
    union all
    select * from 表 where 字段A like 'M%' or 字段B=1 ) xorder by x.字段A, x.字段B