使用 MySQL IN() 显示多个选定行
我们首先创建一个表格 −
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 命令在表格中插入一些记录 −
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)
使用 select 语句从表格中显示所有记录 −
mysql> select *from DemoTable1045;
这将产生以下输出 −
+-----+-----------+ | Id | FirstName | +-----+-----------+ | 100 | John | | 101 | Chris | | 102 | Robert | | 103 | Mike | | 104 | Adam | +-----+-----------+ 5 rows in set (0.00 sec)
以下是用来显示多个选定行的 MySQL 查询 −
mysql> select *from DemoTable1045 tbl where(tbl.Id, tbl.FirstName) IN ((101, 'Chris'), (102, 'Robert'), (104,'Adam'));
这将产生以下输出 −
+-----+-----------+ | Id | FirstName | +-----+-----------+ | 101 | Chris | | 102 | Robert | | 104 | Adam | +-----+-----------+ 3 rows in set (0.05 sec)
广告