SQL 如何写

求一个SQL 怎么写

如下:

一张表 test

a b c d

a1 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 d

a1 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:30

where 是根据d 是否为null 来组合显示。 排序是按照 c的时间来desc 。 谢谢


SQL code

select * 
from 表
order by d, c desc


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


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

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


                        
      
      
                    
SQL 如何写

相关文章:

你感兴趣的文章:

标签云: