How to select records beginning with certain numbers in MySQL?



The optimal solution to select records beginning with certain numbers, use MySQL LIKE operator. Let us first create a table −

mysql> create table DemoTable (    ClientId bigint,    ClientName varchar(40) ); Query OK, 0 rows affected (0.82 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values(23568777,'Chris Brown'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values(9085544,'John Doe'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(9178432,'John Doe'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values(9078482,'David Miller'); Query OK, 1 row affected (0.17 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+----------+--------------+ | ClientId | ClientName   | +----------+--------------+ | 23568777 | Chris Brown  | | 9085544  | John Doe     | | 9178432  | John Doe     | | 9078482  | David Miller | +----------+--------------+ 4 rows in set (0.00 sec)

Following is the query to select records beginning with certain numbers in MySQL −

mysql> select *from DemoTable where ClientId LIKE '90%';

This will produce the following output −

+----------+--------------+ | ClientId | ClientName | +----------+--------------+ | 9085544  | John Doe     | | 9078482 | David Miller | +----------+--------------+ 2 rows in set (0.00 sec)
Updated on: 2019-10-09T10:48:05+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements