要基于子字符串位置对搜索结果进行排序,请使用ORDER BY LOCATE()
。让我们首先创建一个表-
mysql> create table DemoTable1838 ( Subject varchar(100) );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable1838 values('MongoDB MySQL'); mysql> insert into DemoTable1838 values('MySQL Java'); mysql> insert into DemoTable1838 values('JavaWithMySQL');
使用select语句显示表中的所有记录-
mysql> select * from DemoTable1838;
这将产生以下输出-
+---------------+ | Subject | +---------------+ | MongoDB MySQL | | MySQL Java | | JavaWithMySQL | +---------------+ 3 rows in set (0.00 sec)
这是根据子字符串位置对搜索结果进行排序的查询:
mysql> select * from DemoTable1838 where Subject LIKE '%MySQL%' ORDER BY LOCATE('MySQL', Subject);
这将产生以下输出-
+---------------+ | Subject | +---------------+ | MySQL Java | | MongoDB MySQL | | JavaWithMySQL | +---------------+ 3 rows in set (0.00 sec)