只查找和显示MySQL中某一列的重复值一次
我们首先创建一个表 -
mysql> create table DemoTable -> ( -> value int -> ); Query OK, 0 rows affected (0.82 sec)
使用 insert 命令在表中插入一些记录 -
mysql> insert into DemoTable values(10); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values(100); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values(20); Query OK, 1 row affected (0.28 sec) mysql> insert into DemoTable values(10); Query OK, 1 row affected (0.34 sec) mysql> insert into DemoTable values(30); Query OK, 1 row affected (0.24 sec) mysql> insert into DemoTable values(40); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values(20); Query OK, 1 row affected (0.07 sec)
使用 select 语句显示表中的所有记录 -
mysql> select *from DemoTable;
输出
这将生成以下输出 -
+-------+ | value | +-------+ | 10 | | 100 | | 20 | | 10 | | 30 | | 40 | | 20 | +-------+ 7 rows in set (0.00 sec)
以下是查找重复值并显示(去重)它们的查询 -
mysql> select *from DemoTable group by value having count(*) > 1;
输出
这将生成以下输出 -
+-------+ | value | +-------+ | 10 | | 20 | +-------+ 2 rows in set (0.00 sec)
广告