在 MySQL 中实现增量搜索并显示具有特定数字的值?
mysqlmysqli database
为此,您可以使用 SUBSTRING_INDEX()。让我们首先创建一个表 −
mysql> create table DemoTable (Number varchar(100)); Query OK, 0 rows affected (0.60 sec)
示例
使用 insert 命令在表中插入一些记录 −
mysql> insert into DemoTable values('235678'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values('1634990'); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable values('678590'); Query OK, 1 row affected (0.46 sec) mysql> insert into DemoTable values('908765432'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('222343388773'); Query OK, 1 row affected (0.35 sec) mysql> insert into DemoTable values('09028215'); Query OK, 1 row affected (0.51 sec)
使用 select 语句显示表中的所有记录 −
mysql> select *from DemoTable;
输出
+--------------+ | Number | +--------------+ | 235678 | | 1634990 | | 678590 | | 908765432 | | 222343388773 | | 09028215 | +--------------+ 6 rows in set (0.00 sec)
以下是在 MySQL 中获取特定数字的值的查询 −
mysql> select Number from DemoTable where Number LIKE '%5%' ORDER BY LENGTH(SUBSTRING_INDEX(Number, '5', 1));
输出
+-----------+ | Number | +-----------+ | 235678 | | 678590 | | 908765432 | | 09028215 | +-----------+ 4 rows in set (0.00 sec)