1、mysql field自定義排序函數的格式html
order by field(value,str1,str2,str3,str4,,,,,,strn)mysql
其中value後面的參數自定義,不限制參數個數sql
含義函數
將獲取出來的數據根據str1,str2,str3,str4等的順序排序spa
2、mysql order by field 實例code
按 name 以 'Suzi','Ben','Henry' 的順序排列htm
mysql> select * from driver_log order by field(name,'Suzi','Ben','Henry'); --結果 +--------+-------+------------+-------+ | rec_id | name | trav_date | miles | +--------+-------+------------+-------+ | 2 | Suzi | 2006-08-29 | 391 | | 7 | Suzi | 2006-09-02 | 502 | | 1 | Ben | 2006-08-30 | 152 | | 9 | Ben | 2006-09-02 | 79 | | 5 | Ben | 2006-08-29 | 131 | | 8 | Henry | 2006-09-01 | 197 | | 6 | Henry | 2006-08-26 | 115 | | 4 | Henry | 2006-08-27 | 96 | | 3 | Henry | 2006-08-29 | 300 | | 10 | Henry | 2006-08-30 | 203 | +--------+-------+------------+-------+
參考資料:mysql 自定義排序函數field() http://www.studyofnet.com/news/1173.html 排序