MYSQL查询根据名字和姓氏查找重复记录

时间:2016-09-20 11:33:50

标签: php mysql sql

我有一个包含以下列的用户表,我提出了一个查询,根据名字和姓氏识别重复记录。

表格结构

CREATE TABLE IF NOT EXISTS `User` (
  `id` bigint(20) NOT NULL,
  `address1` text COLLATE utf8_unicode_ci,
  `mobile` varchar(15) COLLATE utf8_unicode_ci DEFAULT NULL,
  `name` varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL,
  `firstName` varchar(64) COLLATE utf8_unicode_ci DEFAULT NULL,
  `lastName` varchar(64) COLLATE utf8_unicode_ci DEFAULT NULL,
  `username` varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL,
  `password` varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL,
  `email` varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL,
  `gender` tinyint(2) NOT NULL DEFAULT '0' COMMENT '1 - female, 2-male, 0 - unknown',
  `zip` varchar(15) COLLATE utf8_unicode_ci DEFAULT NULL,
  `countryCode` varchar(3) COLLATE utf8_unicode_ci DEFAULT NULL,
  `joinedDate` datetime DEFAULT NULL,
  `dateOfBirth` date DEFAULT NULL COMMENT 'User date of birth'
) ENGINE=MyISAM AUTO_INCREMENT=63561 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; 

和我的查询

SELECT id, firstName, lastName, mobile, email, count(*) as dupCount 
FROM `User` `t` 
WHERE firstName is not null and lastName is not null 
GROUP BY firstName,lastName HAVING count(*) > 1 
ORDER BY t.joinedDate DESC
下面的

是我运行查询时结果的屏幕截图

enter image description here

我的数据集在http://pastebin.com/Y5qsTcFc

抱歉,如果粘贴垃圾箱不起作用。试试这个https://drive.google.com/file/d/0B3gG6MG9uh7KdmRGT3d0NWg2aVk/view?usp=sharing

当有电子邮件地址的记录为NULL时,我们遇到了问题。我总是收到这个错误。

1062 - 关键'group_key'重复输入'Rachel-Besnainou'

enter image description here

我跑的查询是:

SELECT id, firstName, lastName, mobile, email, count(*) as dupCount 
FROM `User` `t` 
WHERE firstName is not null and lastName is not null 
GROUP BY firstName,lastName 
HAVING count(*) > 1

我不知道为什么会出现这个错误。任何帮助赞赏

更新1

我尝试将此添加到我的数据库并运行查询仍然是相同的

SET SESSION max_heap_table_size=536870912;

SET SESSION tmp_table_size=536870912;

参考:Duplicate entry for key 'group_key'

更新2

我在diff mysql和phpmyadmin版本中尝试了相同的数据库并且它有效。看起来我的mysql版本有一个bug。我的mysql版本是 5.6.26

1 个答案:

答案 0 :(得分:1)

试试这个:

select a1.id, 
a1.FirstName, 
a1.LastName,  
a1.mobile, 
a1.email
from user a1
inner join 
(
  select FirstName, LastName, count(id)
  from User
  where firstname is not null 
  and lastname is not null
  group by FirstName, LastName
  having count(id)>1
) u1
    on u1.FirstName = a1.Firstname
    and u1.Lastname = a1.lastname
order by a1.joineddate