各位大大好  
求一个sql文 例如 有表一张 vip_no  name   city  .....
        jack   china
0002    tom    china
        black  china
0001    white  china
我需要select 以后显示的结果 已order by排序 但是 要求 是  将 有vip_no的人员 显示在前面 但是 我用order by后都是 null的人员 在最前面 不知道 有啥办法解决 显示结果为
0001 white
0002 tom
     jack
     black这个显示不可以必须 0001 在最前面
0002 tom
0001 white
     jack
     black  谢谢

解决方案 »

  1.   

    select * from TB order by vip_no desc
      

  2.   


    order by case when vip_no is null then 10000000 else vip_no end asc
      

  3.   

    order by case vip_no when '0002' then 1 else 0 end 
      

  4.   

    order by case when vip_no is null then 1 else 0 end,vip_no
      

  5.   

    order by case when .... then ... else ... end或者
    select * from tb where ...
    union all
    select * from tb where ...
      

  6.   

    If not object_id('[p]') is null
        Drop table [p]
    Go
    CREATE TABLE [dbo].[P]( id varchar(4),name varchar(20))
    insert p select
    '0003', 'white' union all select 
    '0002' ,'tom' union all select
      ''  ,'jack' union all select
       '' ,'black' 
    select * from p order by case when id is null or id='' then '11111' else id end ascid   name
    ---- --------------------
    0002 tom
    0003 white
         jack
         black(4 行受影响)
      

  7.   

    ----------------------------------------------------------------
    -- Author  :fredrickhu(我是小F,向高手学习)
    -- Date    :2009-09-02 14:00:55
    -- Verstion:
    --      Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86) 
    -- Nov 24 2008 13:01:59 
    -- Copyright (c) 1988-2005 Microsoft Corporation
    -- Developer Edition on Windows NT 5.2 (Build 3790: Service Pack 1)
    --
    ----------------------------------------------------------------
    --> 测试数据:[tb]
    if object_id('[tb]') is not null drop table [tb]
    go 
    create table [tb]([vip_no] varchar(4),[name] varchar(5),[city] varchar(5))
    insert [tb]
    select null,'jack','china' union all
    select '0002','tom','china' union all
    select null,'black','china' union all
    select '0001','white','china'
    --------------开始查询--------------------------select 
      * 
    from 
      [tb] 
    order by 
    case vip_no 
     when '0002' then 0 
     when '0001' then 1 
     else 2 end
    ----------------结果----------------------------
    /* vip_no name  city
    ------ ----- -----
    0002   tom   china
    0001   white china
    NULL   jack  china
    NULL   black china(4 行受影响)
    */
      

  8.   

    select * from tb order by isnull(vip_no,9999)
      

  9.   

    declare @t table (vip_no varchar(10),  name varchar(10) ,city varchar(10) )
    insert into @t
      select null,  'jack'  , 'china'
    union all select '0002'  ,  'tom'   , 'china'
    union all select null,  'black' , 'china'
    union all select '0001'  ,  'white' , 'china'select * 
    from @t
    order by case when vip_no is null then 1 else 0 end,vip_novip_no     name       city       
    ---------- ---------- ---------- 
    0001       white      china
    0002       tom        china
    NULL       black      china
    NULL       jack       china
      

  10.   


    select * from  tb
    where id is not nnull
    order by id
    union all
    select * from tb 
    where id is null