mysql - 对单词中匹配的起始字母进行排序

标签 mysql mysql-workbench

我有一个表,我正在尝试在 name 上使用“apple m”过滤值

select * from table where name like '%apple%m%'
+-------+-------------------------------+------------------------+
|  id   |             name              |          cat           |
+-------+-------------------------------+------------------------+
|  2757 | Apple MacBook                 | Laptops & Accessories  |
|  2777 | Apple Bottom Tops             | Western Wear           |
|  2752 | Apple Monitors                | Desktop Components     |
|  2756 | Apple Desktop & Monitors      | Desktops & Accessories |
|  2778 | Apple Bottom Tunics           | Tops                   |
|  2776 | Apple Selector & Smart Box    | Video & TV Accessories |
|  2787 | Apple Pie Pyjamas             | Girl Clothes           |
|  2773 | Apple Selector & Smart Box    | TV & Video Accessories |
|  2780 | Apple Fun Card Games          | Toys                   |
| 38304 | Snapple Mixer juicer grinders | Kitchen Appliances     |
+-------+-------------------------------+------------------------+

我想对以“apple”和“m”开头的显示值进行排序,如下所示:

+------+--------------------------+------------------------+
|  id  |           name           |          cat           |
+------+--------------------------+------------------------+
| 2757 | Apple MacBook            | Laptops & Accessories  |
| 2752 | Apple Monitors           | Desktop Components     |
| 2756 | Apple Desktop & Monitors | Desktops & Accessories |
|      | ---Rest all after this-- |                        |
+------+--------------------------+------------------------+

最佳答案

用途:

ORDER BY name LIKE 'Apple%m%' DESC, name ASC

name LIKE 'Apple%m%' 当名称与模式匹配时为 1,当名称与模式不匹配时为 0,所以这将首先对匹配的名称进行排序。然后它将按每个组中的名称进行排序。

关于mysql - 对单词中匹配的起始字母进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33620796/

相关文章:

MYSQL导入: Cannot get geometry object from data you send to the GEOMETRY field

MySQL 在 AWS RDS 中插入速度慢

php - 没有表格的 CodeIgniter 分页

php - Laravel 时间戳由 Carbon::parse() 设置

php - 尝试用 MySQL、PHP 和谷歌地图做商店定位器

mysql - 删除 mysql.general_log 表时出错

Python 自然语言处理 : How to store a python list to MySQL database?

mysql - “GRANT ALL ON 数据库。* TO 命令中出现错误

mysql - 将数据库架构与 MySQL Workbench 进行比较

mysql - 如何在SQL中实现聚合? (这与 GroupBy 无关)