admin

如何检查mysql表中多列重复项

sql

我有一张棒球运动员的桌子(全部1000人左右),其中包括:

mysql> describe person;
+-----------+-------------+------+-----+---------+----------------+
| Field     | Type        | Null | Key | Default | Extra          |
+-----------+-------------+------+-----+---------+----------------+
| id        | int(11)     | NO   | PRI | NULL    | auto_increment |
| firstname | varchar(30) | NO   |     | NULL    |                |
| lastname  | varchar(30) | NO   |     | NULL    |                |
+-----------+-------------+------+-----+---------+----------------+

但是我认为有些球员已经加入了两次。我该如何检查特定姓氏,姓氏组合的出现次数?


阅读 148

收藏
2021-05-10

共1个答案

admin

这提供了重复项列表:

SELECT firstname, lastname, COUNT(*) 
FROM person 
GROUP BY firstname, lastname 
HAVING COUNT(*) > 1;

如果要查看每一行的计数,请删除hading子句:

SELECT firstname, lastname, COUNT(*) 
FROM person 
GROUP BY firstname, lastname;
2021-05-10