MySQL query to fetch records with arrangement in the form of numbers and letter like 99S, 50K, etc.?


The easiest way to achieve this is by using REGEXP. Let us first create a table −

mysql> create table DemoTable
(
   Id varchar(50)
);
Query OK, 0 rows affected (0.77 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values('John123');
Query OK, 1 row affected (0.23 sec)
mysql> insert into DemoTable values('123Chris');
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable values('99Bob');
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable values('19David');
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable values('99S');
Query OK, 1 row affected (0.18 sec)
mysql> insert into DemoTable values('10M');
Query OK, 1 row affected (0.14 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+----------+
| Id       |
+----------+
| John123  |
| 123Chris |
| 99Bob    |
| 19David  |
| 99S      |
| 10M      |
+----------+
6 rows in set (0.00 sec)

Following is the query to display records with arrangements in the form of numbers and letter −

mysql> select *from DemoTable where Id regexp '^[0-9][0-9][A-Z]$';

This will produce the following output −

+------+
| Id   |
+------+
| 99S  |
| 10M  |
+------+
2 rows in set (0.00 sec)

Updated on: 03-Oct-2019

123 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements