MySQL 选择值存在多次的位置
mysqlmysqli database
为此,您可以将 GROUP BY HAVING 与 COUNT(*) 函数一起使用。让我们首先创建一个表 −
mysql> create table DemoTable -> ( -> Value int -> ); Query OK, 0 rows affected (0.47 sec)
使用 insert 命令在表中插入一些记录 −
mysql> insert into DemoTable values(20); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values(10); Query OK, 1 row affected (0.08 sec) mysql> insert into DemoTable values(30); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values(10); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values(30); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values(40); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values(50); Query OK, 1 row affected (0.27 sec)
使用 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)