Let us first create a table −
mysql> create table DemoTable ( FirstName varchar(100), LastName varchar(100) ); Query OK, 0 rows affected (0.83 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('Adam','Smith'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('John','Doe'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values('John','Smith'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('Chris','Brown'); Query OK, 1 row affected (0.08 sec) mysql> insert into DemoTable values('Robert','Brown'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values('David','Miller'); 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 −
+-----------+----------+ | FirstName | LastName | +-----------+----------+ | Adam | Smith | | John | Doe | | John | Smith | | Chris | Brown | | Robert | Brown | | David | Miller | +-----------+----------+ 6 rows in set (0.00 sec)
Following is the query to fetch records on the basis of LastName using MySQL IN() −
mysql> select *from DemoTable where LastName IN('Smith','Brown','Miller');
This will produce the following output −
+-----------+----------+ | FirstName | LastName | +-----------+----------+ | Adam | Smith | | John | Smith | | Chris | Brown | | Robert | Brown | | David | Miller | +-----------+----------+ 5 rows in set (0.00 sec)