如何在单个MySQL查询中获取多行?

让我们首先创建一个表-

mysql> create table DemoTable
(
   Id int,
   Name varchar(100)
);

使用插入命令在表中插入一些记录-

mysql> insert into DemoTable values(100,'Chris');
mysql> insert into DemoTable values(110,'David');
mysql> insert into DemoTable values(120,'Robert');
mysql> insert into DemoTable values(130,'Mike');

使用select语句显示表中的所有记录-

mysql> select *from DemoTable;

这将产生以下输出-

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

以下是在单个MySQL查询中获取多行的查询-

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

这将产生以下输出-

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