按照 MySQL 中的特定单词排序
为此,请使用 ORDER BY INSTR()。我们首先创建一个表 −
mysql> create table DemoTable822(Word text); Query OK, 0 rows affected (1.11 sec)
使用 insert 命令在表中插入一些记录 −
mysql> insert into DemoTable822 values('Forever'); Query OK, 1 row affected (0.26 sec) mysql> insert into DemoTable822 values('ever'); Query OK, 1 row affected (1.31 sec) mysql> insert into DemoTable822 values('every'); Query OK, 1 row affected (0.25 sec) mysql> insert into DemoTable822 values('everyday'); Query OK, 1 row affected (0.58 sec)
使用 select 语句从表中显示所有记录 −
mysql> select *from DemoTable822;
这将产生以下输出 −
+----------+ | Word | +----------+ | Forever | | ever | | every | | everyday | +----------+ 4 rows in set (0.00 sec)
以下是对字符串开头或内容中特定单词排序的查询 −
mysql> select *from DemoTable822 where Word LIKE '%ever%' order by INSTR(Word, 'ever'), Word;
这将产生以下输出 −
+----------+ | Word | +----------+ | ever | | every | | everyday | | Forever | +----------+ 4 rows in set (0.00 sec)
广告