在 mysql 表中查找重复记录
本教程探讨如何分别使用 select distinct
和 select count
语句检查 mysql 表中的重复项并检索重复项的数量。
使用 select distinct
语句检查 mysql 表中的重复项
数据库表中有重复记录的影响可能从轻微的不便到灾难。幸运的是,mysql 有一些漂亮的关键字可以组合起来扫描表中的重复项。
此外,我们可以计算重复记录的出现次数,并在必要时将其删除。
让我们使用名为 employee_details
的示例表创建一个 test_company
数据库。
createdatabasetest_company;usetest_company;createtableemployees_details(idintauto_increment,namevarchar(255)notnull,titlevarchar(255)notnull,salaryint,primarykey(id));
该表填充了值,包括重复值。
insertintoemployees_details(name,title,salary)values('james maddison','computer engineer',80000),('matthew defoe','software architect',150000),('daniel jameson','software engineer ii',95000),('jules reddington','senior software engineer',120000),('carlos rodriguez','data engineer',100000),('matthew defoe','software architect',150000),('daniel jameson','software engineer ii',95000),('jules reddington','senior software engineer',120000);select*fromemployees_details;
输出:
---- ------------------ -------------------------- --------
| id | name | title | salary |
---- ------------------ -------------------------- --------
| 1 | james maddison | computer engineer | 80000 |
| 2 | matthew defoe | software architect | 150000 |
| 3 | daniel jameson | software engineer ii | 95000 |
| 4 | jules reddington | senior software engineer | 120000 |
| 5 | carlos rodriguez | data engineer | 100000 |
| 6 | matthew defoe | software architect | 150000 |
| 7 | daniel jameson | software engineer ii | 95000 |
| 8 | jules reddington | senior software engineer | 120000 |
---- ------------------ -------------------------- --------
8 rows in set (0.00 sec)
此表中有三个重复项,由于表的大小较小,因此很容易发现。我们对较大的表使用 select distinct
语句从表中检索唯一记录。
根据官方文档,select distinct
语句仅检索输出记录的一个实例,即使它出现多次。
因此,当 select distinct
语句返回的记录数小于表中的总记录数时,我们可以确定存在重复。
-- retrieving only distinct records.
selectdistinctname,title,salaryfromemployees_details;
输出:
------------------ -------------------------- --------
| name | title | salary |
------------------ -------------------------- --------
| james maddison | computer engineer | 80000 |
| matthew defoe | software architect | 150000 |
| daniel jameson | software engineer ii | 95000 |
| jules reddington | senior software engineer | 120000 |
| carlos rodriguez | data engineer | 100000 |
------------------ -------------------------- --------
5 rows in set (0.00 sec)
请注意,查询中不包含 id
列,因为 mysql 将 id
列标识为唯一记录。
因此,在查询中包含 id
列将返回所有记录(包括重复项)作为唯一记录。
从结果集中,我们可以推断出有三 (3) 条重复记录,因为查询返回五 (5) 条不同的记录并且表中有八 (8) 条记录。
使用 select count
语句查找 mysql 表中重复出现的次数
现在,在检测到重复项的存在后,我们可以使用 select count
语句来查找重复项的出现次数。
selectnameas'employee name',count(*)asoccurrencefromemployees_detailsgroupbynamehavingoccurrence>1;
输出:
------------------ ------------
| employee name | occurrence |
------------------ ------------
| daniel jameson | 2 |
| jules reddington | 2 |
| matthew defoe | 2 |
------------------ ------------
3 rows in set (0.001 sec)
这将检索重复的记录和表中每条记录的重复数。正如预期的那样,有三 (3) 条重复记录。
select count
语句的使用将通过此官方参考进一步讨论。
使用 inner join
语句查看 mysql 表中的重复记录
我们可以在目标表和 select distinct
查询之间使用 inner join
查询来查看主记录旁边的重复记录。
selecti.id,o.name,o.title,o.salaryfromemployees_detailsasiinnerjoin(selectdistinctname,title,salaryfromemployees_details)asooni.name=o.name;
输出:
---- ------------------ -------------------------- --------
| id | name | title | salary |
---- ------------------ -------------------------- --------
| 1 | james maddison | computer engineer | 80000 |
| 2 | matthew defoe | software architect | 150000 |
| 6 | matthew defoe | software architect | 150000 |
| 3 | daniel jameson | software engineer ii | 95000 |
| 7 | daniel jameson | software engineer ii | 95000 |
| 4 | jules reddington | senior software engineer | 120000 |
| 8 | jules reddington | senior software engineer | 120000 |
| 5 | carlos rodriguez | data engineer | 100000 |
---- ------------------ -------------------------- --------
8 rows in set (0.001 sec)
转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处
本文地址:
相关文章
如何在 mysql 中声明和使用变量
发布时间:2024/03/26 浏览次数:115 分类:mysql
-
当你需要在 mysql 中的脚本中存储单个值时,最好的方法是使用变量。变量有不同的种类,有必要知道何时以及如何使用每种类型。
发布时间:2024/03/26 浏览次数:176 分类:mysql
-
本教程演示了如何在 mysql 中重置自动增量。
在 mysql 中使用 mysqladmin 刷新主机解除阻塞
发布时间:2024/03/26 浏览次数:82 分类:mysql
-
你将了解阻止主机的原因。此外,通过使用 phpmyadmin 和命令提示符刷新主机缓存来解除阻塞的不同方法和效果。
发布时间:2024/03/26 浏览次数:199 分类:mysql
-
本教程演示如何在 mysql 中转换为整数。