Display specific name from a table with repeated individual FirstName and LastName using LIKE clause twice


Let us first create a table −

mysql> create table DemoTable
(
   StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   StudentName varchar(30)
);
Query OK, 0 rows affected (0.70 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable(StudentName) values('John Smith');
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable(StudentName) values('David Miller');
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable(StudentName) values('Carol Taylor');
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable(StudentName) values('John Doe');
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable(StudentName) values('Chris Brown');
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable(StudentName) values('Adam Doe');
Query OK, 1 row affected (0.11 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+-----------+---------------+
| StudentId | StudentName   |
+-----------+---------------+
|         1 | John Smith    |
|         2 | David Miller  |
|         3 | Carol Taylor  |
|         4 | John Doe      |
|         5 | Chris Brown   |
|         6 | Adam Doe      |
+-----------+---------------+
6 rows in set (0.00 sec)

Here is the query to select record from table with first name “John” and last name “Doe” −

mysql> select *from DemoTable
   where StudentName LIKE '%John%' and StudentName LIKE '%Doe%';

This will produce the following output −

+-----------+-------------+
| StudentId | StudentName |
+-----------+-------------+
|         4 | John Doe    |
+-----------+-------------+
1 row in set (0.00 sec)

Updated on: 04-Oct-2019

56 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements