For this, you can use LIKE operator along with AND. Let us first create a table −
mysql> create table DemoTable ( EmployeeFirstName varchar(50), EmployeeLastName varchar(50) ); Query OK, 0 rows affected (0.64 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('John','Smith'); Query OK, 1 row affected (0.61 sec) mysql> insert into DemoTable values('David','Miller'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('John','Doe'); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable values('Adam','Smith'); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable values('Carol','Taylor'); Query OK, 1 row affected (0.08 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+-------------------+------------------+ | EmployeeFirstName | EmployeeLastName | +-------------------+------------------+ | John | Smith | | David | Miller | | John | Doe | | Adam | Smith | | Carol | Taylor | +-------------------+------------------+ 5 rows in set (0.00 sec)
Following is the query to search 2 fields at the same time to fetch records with FirstName beginning with “Joh” and LastName beginning with “Sm” −
mysql> select *from DemoTable where EmployeeFirstName LIKE 'Joh%' and EmployeeLastName LIKE 'Sm%';
This will produce the following output −
+-------------------+------------------+ | EmployeeFirstName | EmployeeLastName | +-------------------+------------------+ | John | Smith | +-------------------+------------------+ 1 row in set (0.00 sec)