select * from news order by isnull(newstitle),newsdate desc
一样不能实现

解决方案 »

  1.   

    select * from news order by case when newstitle is null then 0 else 1 end,newsdate desc
      

  2.   

    -- 创建测试环境
    if exists(select 1 from [tempdb]..[sysobjects] where id=object_id('[tempdb]..[#]'))
    drop table [tempdb]..[#]
    go
    create table #
    (
      ID int identity,newstitle varchar(20),newsdate datetime
    )
    insert #(newstitle,newsdate)
    select '新闻1',getdate() union
    select '新闻2','2005-1-2' union
    select '新闻2','2005-1-3' union
    select '','2006-3-2' union
    select null,'2007-3-1'-- 测试
    select * from # order by newstitle,newsdate desc
    select * from # order by (case newstitle when null then 0 else 1 end),newsdate desc--删除测试环境
    drop table #--结果
    /*
    ID          newstitle            newsdate                                               
    ----------- -------------------- ---------------------------------
    1           NULL                 2007-03-01 00:00:00.000
    2                                2006-03-02 00:00:00.000
    3           新闻1                  2005-06-27 13:11:50.653
    5           新闻2                  2005-01-03 00:00:00.000
    4           新闻2                  2005-01-02 00:00:00.000(所影响的行数为 5 行)ID          newstitle            newsdate                                               
    ----------- -------------------- -------------------------------- 
    1           NULL                 2007-03-01 00:00:00.000
    2                                2006-03-02 00:00:00.000
    3           新闻1                  2005-06-27 13:11:50.653
    5           新闻2                  2005-01-03 00:00:00.000
    4           新闻2                  2005-01-02 00:00:00.000(所影响的行数为 5 行)
    */
      

  3.   

    写错了,应加上set ansi_nulls off
    -- 创建测试环境
    if exists(select 1 from [tempdb]..[sysobjects] where id=object_id('[tempdb]..[#]'))
    drop table [tempdb]..[#]
    go
    create table #
    (
      ID int identity,newstitle varchar(20),newsdate datetime
    )
    insert #(newstitle,newsdate)
    select '新闻1',getdate() union
    select '新闻2','2005-1-2' union
    select '新闻2','2005-1-3' union
    select '','2006-3-2' union
    select null,'2007-3-1'-- 测试
    set ansi_nulls off
    select * from # order by newstitle,newsdate desc
    select * from # order by (case newstitle when null then 0 else 1 end),newsdate desc--删除测试环境
    drop table #--结果
    /*
    ID          newstitle            newsdate                                               
    ----------- -------------------- ---------------------------------
    1           NULL                 2007-03-01 00:00:00.000
    2                                2006-03-02 00:00:00.000
    3           新闻1                  2005-06-27 13:11:50.653
    5           新闻2                  2005-01-03 00:00:00.000
    4           新闻2                  2005-01-02 00:00:00.000(所影响的行数为 5 行)ID          newstitle            newsdate                                               
    ----------- -------------------- -------------------------------- 
    1           NULL                 2007-03-01 00:00:00.000
    2                                2006-03-02 00:00:00.000
    3           新闻1                  2005-06-27 13:11:50.653
    5           新闻2                  2005-01-03 00:00:00.000
    4           新闻2                  2005-01-02 00:00:00.000(所影响的行数为 5 行)
    */
      

  4.   

    我的数据库是 access 的,调不好。