如果一个数据表中有两个子段都记录时间,为a,b.
要求:写一个select句子,如果a不为空的时候,按照a的降序排列,如果a为空(NULL),则按b的降序排列,
        select * from [表名] order by  后面应该怎么写?

解决方案 »

  1.   

    写存储过程
    伪代码
    Declare aa变量,bb变量
    select count(a) into aa from table
    if aa>0 then
    select * from table order by a
    else
    select * from table order by b
    end if
      

  2.   

    还有一种方法就是,把你的SQL语句分开写,也就是将楼上所说的存储过程中的语句分开成三步来写,即先进行判断
      

  3.   

    if exsits(select a from table)
      GO
       select * from table order by a desc
      
       select * from table order by b desc
      

  4.   

    问题补充:我是在c#后台代码中(做的是asp.net项目)写这段程序的,有没有好一点的解决方法?谢谢各位大虾
      

  5.   


    string OrderbyWhere=(a==""||a==null)?"order by b":"order by a";
    string strsql="select * from table "+OrderbyWhere;
      

  6.   

    select count(a) into aa from table where a is not nullorder by a/b desc