使用MySQL IN()获得类似的结果。让我们首先创建一个表-
create table DemoTable ( ClientId int, ClientName varchar(100), ClientAge int );
使用插入命令在表中插入一些记录-
insert into DemoTable values(100,'Chris',34); insert into DemoTable values(101,'Robert',31); insert into DemoTable values(103,'David',33); insert into DemoTable values(104,'Mike',45); insert into DemoTable values(105,'Sam',39);
使用select语句显示表中的所有记录-
select *from DemoTable;
这将产生以下输出-
+----------+------------+-----------+ | ClientId | ClientName | ClientAge | +----------+------------+-----------+ | 100 | Chris | 34 | | 101 | Robert | 31 | | 103 | David | 33 | | 104 | Mike | 45 | | 105 | Sam | 39 | +----------+------------+-----------+ 5 rows in set (0.00 sec)
以下是获取类似结果的查询,例如使用MySQL IN()的循环-
select ClientId,ClientName,ClientAge from DemoTable where ClientId IN(101,103,105);
这将产生以下输出-
+----------+------------+-----------+ | ClientId | ClientName | ClientAge | +----------+------------+-----------+ | 101 | Robert | 31 | | 103 | David | 33 | | 105 | Sam | 39 | +----------+------------+-----------+ 3 rows in set (0.00 sec)