在一个表中有photo字段,可能为空,也可能不为空,要求一条sql语句,能够先显示字段不为空的记录,然后列出字段为空的记录,

id photo             name       number
1                    jack        123            
2                    shmit       789
3 pic\pic.gif        join        456要求结果显示 pic\pic.gif       join        456
                   jack        123  
                   shmit       789

解决方案 »

  1.   

    create table tab(id int,photo varchar(20),name varchar(10),number varchar(5))
    insert tab
    select 1,'','jack','123' union all
    select 2,'','shmit','789' union all
    select 3,'pic\pic.gif','join','456'
    select * from tab order by photo desc
    drop table tabid          photo                name       number
    ----------- -------------------- ---------- ------
    3           pic\pic.gif          join       456
    1                                jack       123
    2                                shmit      789
      

  2.   

    本人自己已经解决,看各位有没有什么好的办法,
    select  *  from AA and photo<>''
    union all
    select  *  from AA where and photo=''
    感觉很弱智
      

  3.   

    create table tab(id int,photo varchar(20),name varchar(10),number varchar(5))
    insert tab
    select 1,'','jack','123' union all
    select 2,'','shmit','789' union all
    select 3,'pic\pic.gif','join','456'
    select * from tab where photo <>'' union all
    select * from tab where photo=''
    drop table tabid          photo                name       number
    ----------- -------------------- ---------- ------
    3           pic\pic.gif          join       456
    1                                jack       123
    2                                shmit      789(3 行受影响)
      

  4.   

    这样效率高点:select * from AA order by case photo when '' then 1 else 0 end
      

  5.   

    --原始数据:#T
    create table #T(id int,photo varchar(11),name varchar(5),number int)
    insert #T
    select 1,'','jack',123 union all
    select 2,'','shmit',789 union all
    select 3,'pic\pic.gif','join',456--查询成本:39.43%
    select * from #T order by case photo when '' then 1 else 0 end--查询成本:60.57%
    select * from #T where photo<>''
    union all
    select * from #T where photo=''--删除测试
    drop table #T
      

  6.   

    --原始数据:#T
    create table #T(id int,photo varchar(11),name varchar(5),number int)
    insert #T
    select 1,'','jack',123 union all
    select 2,'','shmit',789 union all
    select 3,'pic\pic.gif','join',456--查询成本:39.43%
    select * from #T order by case photo when '' then 1 else 0 end--查询成本:60.57%
    select * from #T where photo<>''
    union all
    select * from #T where photo=''--删除测试
    drop table #T
      

  7.   

    昨夜小楼
    我上MSN怎么没见到你.牛!!!!!!!
    这样也可以!
      

  8.   

    我菜鸟一个,有什么好的SQL方面的书,推荐以下,我得好好看看!
      

  9.   

    现在都放假了,也懒得上MSN了。我没什么好推荐,唯一推荐的还是《联机丛书》。:(