MySQL query to find same value on multiple fields with LIKE operator?


Let us first create a table −

mysql> create table DemoTable
   ->
   -> (
   -> StudentName varchar(20),
   -> StudentSubject varchar(20)
   -> );
Query OK, 0 rows affected (0.58 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values('John','MySQL');
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable values('Adam','MySQL');
Query OK, 1 row affected (0.09 sec)
mysql> insert into DemoTable values('John','Java');
Query OK, 1 row affected (0.34 sec)
mysql> insert into DemoTable values('Carol','Java');
Query OK, 1 row affected (0.16 sec)
mysql> insert into DemoTable values('John','MongoDB');
Query OK, 1 row affected (0.12 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+-------------+----------------+
| StudentName | StudentSubject |
+-------------+----------------+
| John        | MySQL          |
| Adam        | MySQL          |
| John        | Java           |
| Carol       | Java           |
| John        | MongoDB        |
+-------------+----------------+
5 rows in set (0.00 sec)

Here is the query to find same value on multiple fields with like operator −

mysql> select *from DemoTable
   -> where concat(StudentName,' ',StudentSubject)
   -> like '%John MySQL%';

This will produce the following output −

+-------------+----------------+
| StudentName | StudentSubject |
+-------------+----------------+
| John        | MySQL          |
+-------------+----------------+
1 row in set (0.00 sec)

Updated on: 12-Dec-2019

455 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements