计数表列中重复值数量的 MySQL 查询


我们首先创建一个表 -

mysql> create table DemoTable(
   Data int
);
Query OK, 0 rows affected (0.98 sec)

使用插入命令在表中插入一些记录 -

mysql> insert into DemoTable values(60);
Query OK, 1 row affected (0.27 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.14 sec)
mysql> insert into DemoTable values(60);
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable values(40);
Query OK, 1 row affected (0.11 sec)
mysql> insert into DemoTable values(80);
Query OK, 1 row affected (0.15 sec)

使用 select 语句从表中显示所有记录 -

mysql> select *from DemoTable;

这将产生以下输出 -

+------+
| Data |
+------+
| 60   |
| 40   |
| 50   |
| 60   |
| 40   |
| 80   |
+------+
6 rows in set (0.00 sec)

以下是统计一列中重复值数量的查询 -

mysql> select count(*) from DemoTable group by Data having count(Data) > 1;

这将产生以下输出 -

+----------+
| count(*) |
+----------+
| 2        |
| 2        |
+----------+
2 rows in set (0.03 sec)

更新于:27-9-2019

173浏览

开始你的 职业生涯

通过完成课程获得认证

开始
广告