MySQL 查询查找匹配项并检索记录
要从记录中查找匹配项,请使用 MySQL IN()。我们首先创建一个表 −
mysql> create table DemoTable ( Id int, FirstName varchar(20), Gender ENUM('Male','Female') ); Query OK, 0 rows affected (1.73 sec)
使用 insert 命令在表中插入一些记录 −
mysql> insert into DemoTable values(1,'Chris','Male'); Query OK, 1 row affected (0.47 sec) mysql> insert into DemoTable values(10,'Emma','Female'); Query OK, 1 row affected (1.88 sec) mysql> insert into DemoTable values(9,'Emma','Male'); Query OK, 1 row affected (0.70 sec) mysql> insert into DemoTable values(11,'Isabella','Female'); Query OK, 1 row affected (0.46 sec)
使用 select 语句显示表中的所有记录 −
mysql> select *from DemoTable;
这将生成以下输出 −
+------+-----------+--------+ | Id | FirstName | Gender | +------+-----------+--------+ | 1 | Chris | Male | | 10 | Emma | Female | | 9 | Emma | Male | | 11 | Isabella | Female | +------+-----------+--------+ 4 rows in set (0.00 sec)
以下是查询以查找匹配项并提取 Id 为 1 和 11 的记录 −
mysql> select *from DemoTable where Id IN(1,11);
这将生成以下输出 −
+------+-----------+--------+ | Id | FirstName | Gender | +------+-----------+--------+ | 1 | Chris | Male | | 11 | Isabella | Female | +------+-----------+--------+ 2 rows in set (0.00 sec)
广告