计算重复的ID,并在MySQL的单独列中显示结果

让我们首先创建一个表-

mysql> create table DemoTable1453
   -> (
   -> CustomerId int,
   -> CustomerReviewNumber int
   -> );

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

mysql> insert into DemoTable1453 values(10,4);
mysql> insert into DemoTable1453 values(10,4);
mysql> insert into DemoTable1453 values(11,5);
mysql> insert into DemoTable1453 values(11,5);
mysql> insert into DemoTable1453 values(11,5);
mysql> insert into DemoTable1453 values(13,2);
mysql> insert into DemoTable1453 values(11,5);
mysql> insert into DemoTable1453 values(10,4);
mysql> insert into DemoTable1453 values(13,2);

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

mysql> select * from DemoTable1453;

这将产生以下输出-

+------------+----------------------+
| CustomerId | CustomerReviewNumber |
+------------+----------------------+
|         10 |                    4 |
|         10 |                    4 |
|         11 |                    5 |
|         11 |                    5 |
|         11 |                    5 |
|         13 |                    2 |
|         11 |                    5 |
|         10 |                    4 |
|         13 |                    2 |
+------------+----------------------+
9 rows in set (0.00 sec)

以下是计算重复ID的查询-

mysql> select CustomerId,count(distinct CustomerReviewNumber)
   -> from DemoTable1453
   -> group by CustomerId;

这将产生以下输出-

+------------+--------------------------------------+
| CustomerId | count(distinct CustomerReviewNumber) |
+------------+--------------------------------------+
|         10 |                                    1 |
|         11 |                                    1 |
|         13 |                                    1 |
+------------+--------------------------------------+
3 rows in set (0.14 sec)