如何在带有IN()的MySQL子查询中返回多个结果?
在MySQL中,您可以轻松返回多个结果,也可以使用子查询来实现IN()
。让我们首先创建一个表-
mysql> create table DemoTable1317 -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> Name varchar(20) -> );
使用insert命令在表中插入一些记录
mysql> insert into DemoTable1317(Name) values('Chris Brown'); mysql> insert into DemoTable1317(Name) values('John Doe'); mysql> insert into DemoTable1317(Name) values('Adam Smith'); mysql> insert into DemoTable1317(Name) values('John Smith'); mysql> insert into DemoTable1317(Name) values('David Miller'); mysql> insert into DemoTable1317(Name) values('Carol Taylor');
使用select语句显示表中的所有记录-
mysql> select *from DemoTable1317;
输出结果
+----+--------------+ | Id | Name | +----+--------------+ | 1 | Chris Brown | | 2 | John Doe | | 3 | Adam Smith | | 4 | John Smith | | 5 | David Miller | | 6 | Carol Taylor | +----+--------------+ 6 rows in set (0.00 sec)
以下是在MySQL子查询中返回多个结果的查询-
mysql> select Name from DemoTable1317 -> where Id IN ( select Id from DemoTable1317);
输出结果
+--------------+ | Name | +--------------+ | Chris Brown | | John Doe | | Adam Smith | | John Smith | | David Miller | | Carol Taylor | +--------------+ 6 rows in set (0.00 sec)