实现 MySQL IN 来检索仅选定记录的 2 列
让我们先创建一个表 −
mysql> create table DemoTable810( First int, Second int ); Query OK, 0 rows affected (0.73 sec)
使用 insert 命令在表中插入一些记录 −
mysql> insert into DemoTable810 values(20,40); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable810 values(70,90); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable810 values(120,150); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable810 values(78,128); Query OK, 1 row affected (0.32 sec)
使用 select 语句从表中显示所有记录 −
mysql> select *from DemoTable810 ;
这将产生以下输出 −
+-------+--------+ | First | Second | +-------+--------+ | 20 | 40 | | 70 | 90 | | 120 | 150 | | 78 | 128 | +-------+--------+ 4 rows in set (0.00 sec)
以下是对 2 列实现 MySQL IN 的查询 −
mysql> select *from DemoTable810 where (First,Second) IN((120,150),(20,40));
这将产生以下输出 −
+-------+--------+ | First | Second | +-------+--------+ | 20 | 40 | | 120 | 150 | +-------+--------+ 2 rows in set (0.00 sec)
广告