如下:
一张表 testa  b  c   da1 b1 2012-12-13 20:30:30  null
a2 b2 2012-12-13 20:30:30  2012-12-13 20:30:30
a3 b3 2012-12-13 20:30:30  null
a4 b4 2012-12-13 20:30:30  2012-12-13 20:30:30
a5 b5 2012-12-13 20:30:30  null
a6 b6 2012-12-13 20:30:30  2012-12-13 20:30:30
最后的效果是
a  b  c  da1 b1 2012-12-13 20:30:30  null
a3 b3 2012-12-13 20:30:30  null
a5 b5 2012-12-13 20:30:30  null
a4 b4 2012-12-13 20:30:30  2012-12-13 20:30:30
a2 b2 2012-12-13 20:30:30  2012-12-13 20:30:30
a6 b6 2012-12-13 20:30:30  2012-12-13 20:30:30where 是根据d 是否为null 来组合显示。 排序是按照 c的时间来desc 。  谢谢

解决方案 »

  1.   

    select * 
    from 表
    order by d, c desc
    纯排序嘛,没有什么组合。
      

  2.   

    这样不对哦! c的时间都有,D是有一部没有,有一部有。 要根据D是否为空来排序,分成2组。一组为null,一组为时间。  c 只是一个排序条件而已。 查出来现在在页面的效果是: 点击按钮进去最上面是d为null的数据,下面则是d为时间的。
      

  3.   

    这样不对哦! c的时间都有,D是有一部没有,有一部有。 要根据D是否为空来排序,分成2组。一组为null,一组为时间。  c 只是一个排序条件而已。 查出来现在在页面的效果是: 点击按钮进去最上面是d为null的数据,下面则是d为时间的。
      

  4.   

    不会吧,我试过为 null 的数据会自动排在有时间内容的数据之前的,所以直接用 d 排序就可以。
    如果你一定要确定 null 的排序,可以用这个:
    select *
    from 表
    order by isnull(d) desc, c desc
      

  5.   

    select * from test 
    order by isnull(d) desc,c desc
      

  6.   

    select * from test  
    order by case when d is null then 1 else 2 end,c desc