Ricerca nel sito web

Implementare la ricerca incrementale e visualizzare i valori con un numero specifico in MySQL?


Per questo, puoi utilizzare SUBSTRING_INDEX(). Creiamo innanzitutto una tabella:

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

Esempio

Inserisci alcuni record nella tabella utilizzando il comando di inserimento:

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)

Visualizza tutti i record dalla tabella utilizzando l'istruzione select :

mysql> select *from DemoTable;

Produzione

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

Di seguito è riportata la query per ottenere valori con un numero particolare in MySQL:

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

Produzione

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

Articoli correlati: