How to select records that begin with a specific value in MySQL?


To select records that begin with a specific value, you need to use LIKE operator. Let us first create a table −

mysql> create table DemoTable690(
   UserId int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   UserValue varchar(100)
);
Query OK, 0 rows affected (0.56 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable690(UserValue) values('567890');
Query OK, 1 row affected (0.21 sec)
mysql> insert into DemoTable690(UserValue) values('789032');
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable690(UserValue) values('567342');
Query OK, 1 row affected (0.20 sec)
mysql> insert into DemoTable690(UserValue) values('890678');
Query OK, 1 row affected (0.16 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable690;

This will produce the following output −

+--------+-----------+
| UserId | UserValue |
+--------+-----------+
|      1 | 567890    |
|      2 | 789032    |
|      3 | 567342    |
|      4 | 890678    |
+--------+-----------+
4 rows in set (0.00 sec)

Following is the query to select records that begin with a specific value −

mysql> select *from DemoTable690 where UserValue LIKE '567%';

This will produce the following output −

+--------+-----------+
| UserId | UserValue |
+--------+-----------+
|      1 | 567890    |
|      3 | 567342    |
+--------+-----------+
2 rows in set (0.00 sec)

Updated on: 26-Aug-2019

844 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements