在查询中,想讲符合某条件的某行数据始终显示在最后,而且不希望它参与前面数据的排序。
请问如何写这样的SQL语句?

解决方案 »

  1.   

    那就拼接吧,应该能符合要求,如
    select * from tablename where id != 'aa' order by id 
    union select * from tablename where id = 'aa'
      

  2.   

    那就用union关键字吧。两个集合拼起来,但要注意的是第二个要排序的结果集要排除第一个结果集的记录。
      

  3.   

    用union all连接该数据是排在最后了。可是要查询好几天数据,有关它的全部在最后。要求它在每天的数据的最后一行。也就是时间还是要排序的。真晕?!请教写法?
      

  4.   

    select * from tablename
    where id not like '%%'
    union
    select * from tablename
    where id  like '%%'
      

  5.   

    --先认为设置个排序号,再排序
    select dataCol,serialno,... from
    (select 1 as serialno, dataCol,table.* from table where ... and not (特殊条件)
    union 
    select 2 as serialno, dataCol,table.* from table where 特殊条件
    ) xxx
    order by dataCol,serialno asc (如果有其他排序字段,加到这后边)
      

  6.   

    1 2013-6-24 1
    2 2013-6-24 2
    3 2013-6-24 6
    4 2013-6-24 364
    5 2013-6-24 394
    6 2013-6-24 4481
    7 2013-6-25 1
    8 2013-6-25 2
    9 2013-6-25 6
    10 2013-6-25 364
    11 2013-6-25 394
    12 2013-6-25 4481
    13 2013-6-24 371
    14 2013-6-25 371
    用union连接后是如上结果的,但希望371这个特殊数据,分别排在6-24和6-25最后。
      

  7.   

    oracle的order by 足够灵活
    就这么个意思:
    order by case when xh=371 then null else sj end nulls last,sj--
    或者不需要nulls last,也可以故意把sj设置一个升序中的绝对大的值(如果是升序排序)