Display multiple selected rows using MySQL IN()

MySQLMySQLi Database

Let us first create a table −

mysql> create table DemoTable1045
(
   Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   FirstName varchar(100)
)AUTO_INCREMENT=100;
Query OK, 0 rows affected (0.62 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable1045(FirstName) values('John');
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable1045(FirstName) values('Chris');
Query OK, 1 row affected (0.27 sec)
mysql> insert into DemoTable1045(FirstName) values('Robert');
Query OK, 1 row affected (0.10 sec)
mysql> insert into DemoTable1045(FirstName) values('Mike');
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable1045(FirstName) values('Adam');
Query OK, 1 row affected (0.08 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable1045;

This will produce the following output −

+-----+-----------+
| Id  | FirstName |
+-----+-----------+
| 100 | John      |
| 101 | Chris     |
| 102 | Robert    |
| 103 | Mike      |
| 104 | Adam      |
+-----+-----------+
5 rows in set (0.00 sec)

Following is the MySQL query to display multiple selected rows −

mysql> select *from DemoTable1045 tbl where(tbl.Id, tbl.FirstName) IN ((101, 'Chris'), (102, 'Robert'), (104,'Adam'));

This will produce the following output −

+-----+-----------+
| Id  | FirstName |
+-----+-----------+
| 101 | Chris     |
| 102 | Robert    |
| 104 | Adam      |
+-----+-----------+
3 rows in set (0.05 sec)
raja
Published on 30-Sep-2019 07:56:55
Advertisements