How do you select from MySQL where last value in a string = x?


You can use LIKE operator with wildcards to select the records where last value in a string = x, for example ‘10’, ‘15’, etc.

Let us first create a table −

mysql> create table DemoTable
(
   ClientId varchar(20)
);
Query OK, 0 rows affected (0.68 sec)

Insert records in the table using insert command −

mysql> insert into DemoTable values('CLI-101');
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable values('CLI-110');
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable values('CLI-201');
Query OK, 1 row affected (0.18 sec)
mysql> insert into DemoTable values('CLI-210');
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable values('CLI-502');
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable values('CLI-1010');
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable values('CLI-1012');
Query OK, 1 row affected (0.15 sec)

Following is the query to display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+----------+
| ClientId |
+----------+
| CLI-101  |
| CLI-110  |
| CLI-201  |
| CLI-210  |
| CLI-502  |
| CLI-1010 |
| CLI-1012 |
+----------+
7 rows in set (0.00 sec)

Following is the query to select from MySQL where last value in a string = 10 or 12.

mysql> select *from DemoTable where ClientId like '%10' or ClientId like '%12';

This will produce the following output −

+----------+
| ClientId |
+----------+
| CLI-110  |
| CLI-210  |
| CLI-1010 |
| CLI-1012 |
+----------+
4 rows in set (0.00 sec)

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 30-Jul-2019

143 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements