
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
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)
- Related Articles
- SELECT where row value contains string in MySQL?
- MySQL Select where values IN List string?
- Select from table where value does not exist with MySQL?
- How to select last 10 rows from MySQL?
- MySQL Select Rows where two columns do not have the same value?
- How to select data in MySQL where a field has a minimum value?
- Update only a single value from a MySQL table where select from same table ordered in descending order?
- MySQL Select where value exists more than once
- How to get last 12 digits from a string in MySQL?
- How to select last row in MySQL?
- How to select first and last data row from a MySQL result?
- How to cast and update a numeric value from string column only where applicable in MySQL?
- How to select record from last 6 months in a news table using MySQL?
- How to select all rows from a table except the last one in MySQL?
- How to select last two rows in MySQL?

Advertisements