
- 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 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)
- Related Articles
- How to search specific word in MySQL with RegExp?
- Search by specific field in MongoDB
- How to search a MySQL table for a specific string?
- How to Order by a specific string in MySQL?
- How to search a record by date in MySQL table?
- Anagram Pattern Search
- How to search for a pattern in a Java string?
- How to search a string for a pattern in JavaScript?
- How to search for the specific service in PowerShell?
- MySQL Regex to match a pattern for ignoring a character in search like Chris.Brown?
- Search for specific characters within a string with MySQL?
- How to search a word by capital or small letter in MySQL?
- MySQL Order By specific strings?
- How to search between columns in MySQL?
- How to search multiple columns in MySQL?

Advertisements