现在数据库中譬如说有两个字段用户姓名(yhxm)、最后修改时间(zhxgsj),但是最后修改时间有的是null,有的不是,我现在想要的结果是,按照最后修改时间排序,时间离今天越近的在最上面,然后是离今天越远的,最后是为null的,如果按照最后修改时间asc、desc都不行,大家有没有什么比较好的办法

解决方案 »

  1.   

    ORDER BY zhxgsj DESC nulls last
      

  2.   

    order by zhxgsj desc nulls last
      

  3.   

    select * from your_table order by zhxgsj desc nulls last;
      

  4.   

    DESC NULLS LAST  正解
      

  5.   

    --null默认就是按照最大的处理,为什么要加nulls last?
    Connected to Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 
    Connected as SYS
    SQL> select * from scott.a; COL1  COL2 COL4
    ----- ----- ------------------------------
        1     2 2
        1     2 2
        1     2 2
        1     2 2
        1     2 22
        1     2 22
        1     2 227 rows selectedSQL> desc a;
    Object a does not exist.SQL> desc scott.a;
    Name Type      Nullable Default Comments 
    ---- --------- -------- ------- -------- 
    COL1 NUMBER(4) Y                         
    COL2 NUMBER(4) Y                         
    COL4 CHAR(30)  Y                         SQL> insert into scott.a values(1,3,null);1 row insertedSQL> insert into scott.a values(1,4,null);1 row insertedSQL> insert into scott.a values(1,6,null);1 row insertedSQL> insert into scott.a values(7,6,null);1 row insertedSQL> commit;Commit completeSQL> select * from scott.a; COL1  COL2 COL4
    ----- ----- ------------------------------
        1     2 2
        1     2 2
        1     2 2
        1     2 2
        1     3 
        1     4 
        1     6 
        7     6 
        1     2 22
        1     2 22
        1     2 2211 rows selectedSQL> select * from scott.a order by col4 desc; COL1  COL2 COL4
    ----- ----- ------------------------------
        1     4 
        7     6 
        1     6 
        1     3 
        1     2 22
        1     2 22
        1     2 22
        1     2 2
        1     2 2
        1     2 2
        1     2 211 rows selectedSQL> select * from scott.a order by col4 asc; COL1  COL2 COL4
    ----- ----- ------------------------------
        1     2 2
        1     2 2
        1     2 2
        1     2 2
        1     2 22
        1     2 22
        1     2 22
        7     6 
        1     6 
        1     3 
        1     4 11 rows selectedSQL>