为此,您可以将GROUP BY HAVING子句与一起使用IN()
。让我们首先创建一个表-
mysql> create table DemoTable1885 ( FirstName varchar(20), Subject varchar(50) );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable1885 values('John','MySQL'); mysql> insert into DemoTable1885 values('John','MongoDB'); mysql> insert into DemoTable1885 values('Carol','MySQL'); mysql> insert into DemoTable1885 values('David','Java');
使用插入命令在表中显示一些记录-
mysql> select * from DemoTable1885;
这将产生以下输出-
+-----------+---------+ | FirstName | Subject | +-----------+---------+ | John | MySQL| | John | MongoDB| | Carol | MySQL| | David | Java | +-----------+---------+ 4 rows in set (0.00 sec)
这是当列必须满足多个值时选择行的查询:
mysql> select FirstName from DemoTable1885 where Subject IN('MySQL','MongoDB') group by FirstName having count(*)=2;
这将产生以下输出-
+-----------+ | FirstName | +-----------+ | John | +-----------+ 1 row in set (0.00 sec)