MySQL查询找到匹配项并获取记录
要从记录中查找匹配项,请使用MySQLIN()
。让我们首先创建一个表-
mysql> create table DemoTable ( Id int, FirstName varchar(20), Gender ENUM('Male','Female') );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable values(1,'Chris','Male'); mysql> insert into DemoTable values(10,'Emma','Female'); mysql> insert into DemoTable values(9,'Emma','Male'); mysql> insert into DemoTable values(11,'Isabella','Female');
使用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)
以下是查找与ID1和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)