有N条记录是用 select +union 拼起来的,怎么能让系统不要给排序啊(从第一条select语句开始顺序显示)
比如(数据都是从同一张表查出来的):
 select  '6'
 union
  select '3'
 union
 select '4'
  结果就是:
 6
 3
 4    
  而不是:3,4,6或 6,4,3

解决方案 »

  1.   

    select  '6' 
    union 
      select '3' 
    union 
    select '4' /*
    ----
    3
    4
    6
    */
      

  2.   

    select * from(
    select  '6'  as a
    union 
      select '3' 
    union 
    select '4' ) a/*
    ----
    3
    4
    6
    */
    这样应该排好啦 
      

  3.   

    看错了select * from(
    select  '6'  as a
    union 
      select '3' 
    union 
    select '4' ) a
    order by CHARINDEX(RTRIM(a),'6,4,3')
    /*
    a
    ----
    6
    4
    3
    */
      

  4.   

    select  '6' 
    union all
      select '3' 
    union all
    select '4' 
      

  5.   

    select * from(
    select  '6'  as a
    union 
      select '3' 
    union 
    select '4' ) a
    order by CHARINDEX(RTRIM(a),'6,3,4')
    /*
    a
    ----
    6
    3
    4
    */
      

  6.   

    select * from tb order by charindex(col,'6,3,4')
      

  7.   


    select A from (
    select  A='6',flag=1 
    union 
      select '3',2 
    union 
    select '4',3)t order by flag
    /*
    A
    ----
    6
    3
    4(3 行受影响)*/
      

  8.   

    select  '6' 
    union all
      select '3' 
    union all
    select '4'
    ----
    6
    3
    4(3 row(s) affected) 
      

  9.   


    用union all就会看你的select 顺序显示select  '6'  as a
    union all
      select '3' 
    union all
    select '4'a    
    ---- 
    6
    3
    4(所影响的行数为 3 行)================================select  '6'  as a
    union 
      select '3' 
    union 
    select '4' a    
    ---- 
    3
    4
    6(所影响的行数为 3 行)
      

  10.   

    select * from tb order by charindex(col,'6,3,4')
      

  11.   


    select * from(
    select  '6'  as a
    union 
      select '3' 
    union 
    select '4' 
    union 
    select '3'
    ) aunion在进行表链接后会筛选掉重复的记录,所以在表链接后会对所产生的结果集进行排序运算,删除重复的记录再返回结果。怎么办?
      

  12.   


    select * from (
    select '3'  as a union all
    select '8' union all
    select '4' )  a order by a
      

  13.   


    这个想法不错。不过,如果数据量比较大的情况下,这个就不好做了,并且在无法预知数据的前提下,也要通过变量累计的方法才行,并且可能出现值部分相同情况(如,'6','3','4','35',这样,35就会在4的前面,排序就不正确了)如果每次只select一个值,可以使用临时表,并给临时加上标志位id,用它做排序。如果是多个,那.......我也不知道了。
      

  14.   

    用UNION ALL 呀
    select  '6' 
    union all
      select '3' 
    union all
    select '4'     
    ---- 
    6
    3
    4(所影响的行数为 3 行)
      

  15.   

    我错了,做了实验,35,不会排序在4的前面,中间有逗号,向feixianxxx和以上各位朋友道歉
      

  16.   

    用union all,但union all不会去重复项.
    用如要去重复项,用union,但结果要再嵌套一个查询,order by 来排序.
      

  17.   

    UNION ALL可以实现你的愿望