- 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 to search by specific pattern in MySQL?
You can use regular expression for this. Let us first create a table −
mysql> create table DemoTable ( UserId varchar(100) ); Query OK, 0 rows affected (1.28 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('User-123-G'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('Us-453-GO'); Query OK, 1 row affected (0.08 sec) mysql> insert into DemoTable values('TRUE-908-K'); Query OK, 1 row affected (0.20 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+------------+ | UserId | +------------+ | User-123-G | | Us-453-GO | | TRUE-908-K | +------------+ 3 rows in set (0.00 sec)
Following is the query to search by specific pattern in MySQL. Here we have set the pattern to be 4 alnum – 3 alnum – 1alnum.
Note − Here – is hyphen −
mysql> select *from DemoTable where UserId regexp '^[[:alnum:]]{4}-[[:alnum:]]{3}-[[:alnum:]]{1}$';
This will produce the following output −
+------------+ | UserId | +------------+ | User-123-G | | TRUE-908-K | +------------+ 2 rows in set (0.12 sec)
Advertisements