Implementing incremental search and display the values with a specific number in MySQL?


For this, you can use SUBSTRING_INDEX(). Let us first create a table −

mysql> create table DemoTable (Number varchar(100));
Query OK, 0 rows affected (0.60 sec)

Example

Insert some records in the table using insert command −

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)

Display all records from the table using select statement −

mysql> select *from DemoTable;

Output

+--------------+
| Number       |
+--------------+
| 235678       |
| 1634990      |
| 678590       |
| 908765432    |
| 222343388773 |
| 09028215     |
+--------------+
6 rows in set (0.00 sec)

Following is the query to get values with a particular number in MySQL −

mysql> select Number from DemoTable where Number LIKE '%5%' ORDER BY LENGTH(SUBSTRING_INDEX(Number, '5', 1));

Output

+-----------+
| Number    |
+-----------+
| 235678    |
| 678590    |
| 908765432 |
| 09028215  |
+-----------+
4 rows in set (0.00 sec)

Updated on: 02-Jul-2020

94 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements