How to get multiple rows in a single MySQL query?


Let us first create a table −

mysql> create table DemoTable
(
   Id int,
   Name varchar(100)
);
Query OK, 0 rows affected (0.60 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values(100,'Chris');
Query OK, 1 row affected (0.16 sec)
mysql> insert into DemoTable values(110,'David');
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable values(120,'Robert');
Query OK, 1 row affected (0.17 sec)
mysql> insert into DemoTable values(130,'Mike');
Query OK, 1 row affected (0.13 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+------+--------+
|   Id | Name   |
+------+--------+
|  100 | Chris  |
|  110 | David  |
|  120 | Robert |
|  130 | Mike   |
+------+--------+
4 rows in set (0.00 sec)

Following is the query to get multiple rows in a single MySQL query −

mysql> select *from DemoTable where Id IN(100,120,130):

This will produce the following output −

+------+--------+
|   Id | Name   |
+------+--------+
|  100 | Chris  |
|  120 | Robert |
|  130 | Mike   |
+------+--------+
3 rows in set (0.02 sec)

Updated on: 30-Sep-2019

497 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements