- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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)
Advertisements