How to use RLIKE/REGEXP patterns in MySQL?


Let’s say we want records whenever a column value starts with 2 vowel letters. For that, below is the syntax −

select *from yourTableName where yourColumnName RLIKE '^[aeiouAEIOU][aeiouAEIOU]';

Let us first create a table −

mysql> create table DemoTable664(CityName varchar(100));
Query OK, 0 rows affected (0.89 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable664 values('Springfield');
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable664 values('Austin');
Query OK, 1 row affected (0.30 sec)
mysql> insert into DemoTable664 values('Franklin');
Query OK, 1 row affected (0.16 sec)
mysql> insert into DemoTable664 values('OAKLAND');
Query OK, 1 row affected (0.11 sec)
mysql> insert into DemoTable664 values('Anchorage');
Query OK, 1 row affected (0.18 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable664;

This will produce the following output −

+-------------+
| CityName    |
+-------------+
| Springfield |
| Austin      |
| Franklin    |
| OAKLAND     |
| Anchorage   |
+-------------+
5 rows in set (0.00 sec)

Here is the query to use RLIKE/REGEXP pattern and display records whenever a column value starts with 2 vowel letters −

mysql> select *from DemoTable664 where CityName RLIKE '^[aeiouAEIOU][aeiouAEIOU]';

This will produce the following output −

+----------+
| CityName |
+----------+
| Austin   |
| OAKLAND  |
+----------+
2 rows in set (0.00 sec)
raja
Published on 23-Aug-2019 16:34:06
Advertisements