我有一个棒球运动员表(全部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 | |
+-----------+-------------+------+-----+---------+----------------+
但我认为有些球员已经两次加入。我如何查看特定名字,姓氏组合的出现次数?
答案 0 :(得分:60)
这提供了重复列表:
SELECT firstname, lastname, COUNT(*)
FROM person
GROUP BY firstname, lastname
HAVING COUNT(*) > 1;
如果要查看每行的计数,请删除having子句:
SELECT firstname, lastname, COUNT(*)
FROM person
GROUP BY firstname, lastname;
答案 1 :(得分:4)
SELECT firstname, lastname, count(id) count
FROM person
WHERE firstname = ?
AND lasttname = ?
GROUP BY firstname, lastname
答案 2 :(得分:2)
对于按减少的份数值排序的列表:
SELECT firstname, lastname, COUNT(*) AS n
FROM person
GROUP BY firstname, lastname
ORDER BY n DESC
HAVING n > 1
HAVING
子句是关键部分 - 必须在 GROUP BY
子句后过滤结果,因为WHERE
子句过滤掉行之前他们被分组。
答案 3 :(得分:1)
要获取重复名称和名称的ID,请执行以下操作:
SELECT p1.id, p1.firstname, p1,lastname FROM person p1
INNER JOIN person p2 ON (p1.firstname = p2.firstname
AND p1.lastname = p1.lastname
AND p1.id <> p2.id);
答案 4 :(得分:1)
如果您只想删除所有副本,可以使用临时表格填写所有re data except the duplicate and them re-update you
主表格。
选择具有重复数据的查询将是
SELECT DISTINCT firstname, lastname FROM table
获取表格中的完整数据列表
SELECT firstname, lastname, COUNT(*) AS n
FROM person
GROUP BY firstname, lastname
ORDER BY lastname DESC
HAVING n > 1
使用最后一个查询,您将获得按姓氏字节排序的数据列表。
答案 5 :(得分:1)
To find duplicate records (ex: to find login name and password combination of duplicate records) in a table use the below query;
SELECT em.* FROM employee_master AS em JOIN
(SELECT emp.login, emp.password, COUNT(*)
FROM employee_master emp
WHERE emp.login != '' AND emp.password != ''
GROUP BY emp.login, emp.PASSWORD
HAVING COUNT(*) > 1
) AS dl
WHERE em.login = dl.login AND em.password = dl.password;