MySQL pattern matching 3 or more “a's” in name?

MySQLMySQLi Database

Following is the syntax −

select * from yourTableName where yourColumnName like '%a%a%a%';

Let us first create a −

mysql> create table DemoTable1393
   -> (
   -> CountryName varchar(40)
   -> );
Query OK, 0 rows affected (0.71 sec)

Insert some records in the table using insert −

mysql> insert into DemoTable1393 values('andorra');
Query OK, 1 row affected (0.50 sec)
mysql> insert into DemoTable1393 values('australia');
Query OK, 1 row affected (0.10 sec)
mysql> insert into DemoTable1393 values('argentina');
Query OK, 1 row affected (0.46 sec)
mysql> insert into DemoTable1393 values('austria');
Query OK, 1 row affected (0.26 sec)

Display all records from the table using select −

mysql> select * from DemoTable1393;

This will produce the following output −

+-------------+
| CountryName |
+-------------+
| andorra     |
| australia   |
| argentina   |
| austria     |
+-------------+
4 rows in set (0.00 sec)

Following is the query to pattern matching 3 or more “a's” in column −

mysql> select * from DemoTable1393 where CountryName like '%a%a%a%';

This will produce the following output −

+-------------+
| CountryName |
+-------------+
| australia   |
+-------------+
1 row in set (0.00 sec)
raja
Published on 11-Nov-2019 14:59:33
Advertisements