Using LIKE clause twice in a MySQL query


Let us first create a table −

mysql> create table DemoTable2009
(
   Name varchar(20)
);
Query OK, 0 rows affected (0.51 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable2009 values('John Doe');
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable2009 values('Adam Smith');
Query OK, 1 row affected (0.23 sec)
mysql> insert into DemoTable2009 values('John Smith');
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable2009 values('David Miller');
Query OK, 1 row affected (0.18 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable2009;

This will produce the following output −

+--------------+
| Name         |
+--------------+
| John Doe     |
| Adam Smith   |
| John Smith   |
| David Miller |
+--------------+
4 rows in set (0.00 sec)

Here is the query to use LIKE clause twice in a MySQL query −

mysql> select * from DemoTable2009
   where Name like '%Smith%'
   or Name like '%David%';

This will produce the following output −

+--------------+
| Name         |
+--------------+
| Adam Smith   |
| John Smith   |
| David Miller |
+--------------+
3 rows in set (0.00 sec)

Updated on: 02-Jan-2020

214 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements