为此,可以使用GROUP BY HAVING子句。让我们首先创建一个表-
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, Value int );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable(Value) values(600); mysql> insert into DemoTable(Value) values(600); mysql> insert into DemoTable(Value) values(800); mysql> insert into DemoTable(Value) values(800);
使用select语句显示表中的所有记录-
mysql> select *from DemoTable;
这将产生以下输出-
+----+-------+ | Id | Value | +----+-------+ | 1 | 600 | | 2 | 600 | | 3 | 800 | | 4 | 800 | +----+-------+ 4 rows in set (0.00 sec)
这是查询以选择具有两个精确值的记录-
mysql> select Value from DemoTable tbl where Id IN(1,2) group by Value having count(distinct Id)=2;
这将产生以下输出-
+-------+ | Value | +-------+ | 600 | +-------+ 1 row in set (0.07 sec)