Home >Database >Mysql Tutorial >MySQL自定义排序函数FIELD()_MySQL

MySQL自定义排序函数FIELD()_MySQL

WBOY
WBOYOriginal
2016-06-01 13:17:221015browse

MySQL可以通过field()函数自定义排序,格式:field(value,str1,str2,str3,str4),value与str1、str2、str3、str4比较,返回1、2、3、4,如遇到null或者不在列表中的数据则返回0.

 


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 |

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

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn