1. 程式人生 > >mysql 使用order by field() 自定義排序

mysql 使用order by field() 自定義排序



一、mysql field自定義排序函式的格式

order by field(value,str1,str2,str3,str4,,,,,,strn)

其中value後面的引數自定義,不限制引數個數

含義

將獲取出來的資料根據str1,str2,str3,str4等的順序排序

二、mysql order by field 例項

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 |

+--------+-------+------------+-------+