1. 程式人生 > >mysql中order by 可以指定排序

mysql中order by 可以指定排序

mysql表的排序 ORDER BY field1 [ASC [DESC]], [field2…] [ASC [DESC]],…

這裡是想讓它以 num 排序 但是 num=-1的那條資料單獨為最大值
法一:field( field1,val1,val2,…)

這裡大小順序是 … >val2 >val1 >field1(其他的資料) 子後向前=》從大到小
order by field 時 Using filesort(一種速度比較慢的外部排序) , 一般不建議使用

select * from test order by field(num,-1) asc,num asc;

法二:case when … then … when … then … else … end
select * from test order by case when num = -1 then 1 else 0 end desc;

資料分為2個部分:
我認為 表中的資料分為兩部分, num=-1的一條拿出來作為最大值(特殊部分),然後其他的作為一個整體(其他部分) 然後2個部分進行排序,再用 進行一次排序可達到想要的效果
方法中的2個例子結果是一樣的,但是想對來說第二種用出多些,因為它可以自由選擇大、小的作為特殊部分

#表 test 共有2個欄位 id,name
mysql> desc test;
+-------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra | +-------+------------------+------+-----+---------+----------------+ | id | int(10) unsigned | NO | PRI | NULL | auto_increment | | num | int(11) | NO | | 0 | | +-------+------------------+------+-----+---------+----------------+ mysql> select * from test; +----+-----+ | id | num | +----+-----+ | 1 | -1 | | 2 | 0 | | 3 | 1 | | 4 | 2 | | 5 | 3 | | 6 | 4 | | 7 | 5 | +----+-----+
    #兩種方法獲取到相同的排序 num=-1 作為最大值排序
    mysql> select * from test order by field(num,-1) desc;
    mysql> select * from test order by case when num = -1 then 1 else 0 end desc;
    +----+-----+
    | id | num |
    +----+-----+
    |  1 |  -1 |
    |  2 |   0 |
    |  3 |   1 |
    |  4 |   2 |
    |  5 |   3 |
    |  6 |   4 |
    |  7 |   5 |
    +----+-----+

    mysql> select * from test order by field(num,-1) asc;
    mysql> select * from test order by case when num = -1 then 1 else 0 end asc;
    +----+-----+
    | id | num |
    +----+-----+
    |  2 |   0 |
    |  3 |   1 |
    |  4 |   2 |
    |  5 |   3 |
    |  6 |   4 |
    |  7 |   5 |
    |  1 |  -1 |
    +----+-----+

    #當進行二次排序時,得到了想要的結果
    mysql> select * from test order by field(num,-1) desc,num desc;
    mysql> select * from test order by case when num = -1 then 1 else 0 end desc, num desc;
    +----+-----+
    | id | num |
    +----+-----+
    |  1 |  -1 |
    |  7 |   5 |
    |  6 |   4 |
    |  5 |   3 |
    |  4 |   2 |
    |  3 |   1 |
    |  2 |   0 |
    +----+-----+

    mysql> select * from test order by field(num,-1) asc,num asc;
    mysql> select * from test order by case when num = -1 then 1 else 0 end asc, num asc;

    +----+-----+
    | id | num |
    +----+-----+
    |  2 |   0 |
    |  3 |   1 |
    |  4 |   2 |
    |  5 |   3 |
    |  6 |   4 |
    |  7 |   5 |
    |  1 |  -1 |
    +----+-----+

    #這裡是 num=2 作為最大值進行倒序排序 所以num=2在最前面
    mysql> select * from test order by case when num = 2 then 1 else 0 end desc;
    +----+-----+
    | id | num |
    +----+-----+
    |  4 |   2 |
    |  1 |  -1 |
    |  2 |   0 |
    |  3 |   1 |
    |  5 |   3 |
    |  6 |   4 |
    |  7 |   5 |
    +----+-----+

    # 這裡是 num=2 作為最小值進行正序排序 所以num=2在最前面
    mysql> select * from test order by case when num = 2 then 0 else 1 end;
    +----+-----+
    | id | num |
    +----+-----+
    |  4 |   2 |
    |  1 |  -1 |
    |  2 |   0 |
    |  3 |   1 |
    |  5 |   3 |
    |  6 |   4 |
    |  7 |   5 |
    +----+-----+