多次出现的 MySQL Select where 值
为此,可将 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)
广告