为此,您可以将GROUP BY HAVING与COUNT(*)函数一起使用。让我们首先创建一个表-
mysql> create table DemoTable -> ( -> Value int -> );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable values(20); mysql> insert into DemoTable values(10); mysql> insert into DemoTable values(30); mysql> insert into DemoTable values(10); mysql> insert into DemoTable values(30); mysql> insert into DemoTable values(40); mysql> insert into DemoTable values(50);
使用select语句显示表中的所有记录-
mysql> select *from DemoTable;
这将产生以下输出-
+-------+ | Value | +-------+ | 20 | | 10 | | 30 | | 10 | | 30 | | 40 | | 50 | +-------+ 7 rows in set (0.00 sec)
以下是选择值存在多个位置的查询-
mysql> select *from DemoTable -> group by Value -> having count(*) > 1;
这将产生以下输出-
+-------+ | Value | +-------+ | 10 | | 30 | +-------+ 2 rows in set (0.38 sec)