sql 查找多字段相同记录

来源:百度知道 编辑:UC知道 时间:2024/06/14 14:57:10
一个人员表。字段有
id,name,birthday,areaID,mobile,......

由于插入数据时判断相同数据太严格导致有不少相同数据。所以:

在这个人员表中,列出所有 name,birthday,areaID,mobile 字段记录相同的数据 最好可以按照名字排序。
我写了几个都是查询超级慢的。数据有两万多。数据库是mysql。
===========
给个语句能在数据库工具里面快速查出来就行了
=========================================
tj_angela - 校尉 十一级 那个方法我已经用过了,但是相同数据只能列出一条,需求是列出所有。

呃,我用笨方法,等了半个小时,终于查出来了。。。
==========================================
纽约小秦 - 名动一方 六级
你的那种格式我也试过了,原来多个字段用in要加括号啊。我只用一个name in (。。。)查了半个小时。格式和你的一样。
======================
axislover - 助理 四级
大哥,你的句子能不能改一下,查出某些字段相同的记录的 完整信息。
比如 A 和 B 的信息中名字生日地区相同,我要查出 A,和B的完整信息。 谢谢了,写好会再加分。
其他几位大哥的句子试过了,都很慢,没有十几分查不出来的。

select a.* from tbname a, (select name, birthday, areaID, mobile from tbname group by b.name, b.birthday, b.areaID, b.mobile having count(*) > 1) b
where a.name= b.name and a.birthday= b.birthday and a.areaID= b.areaID and a.mobile = b.mobile

mysql语句和TSQL语句一样吧。

select name,birthday,areaID,mobile,count(*)
from tablename
group by name,birthday,areaID,mobile
having count(*)>=2
order by name

上面这位的query可以查出分别有多少条重复,但是应该不能都列出来,我做了一点点改进,不过速度问题就要靠你反馈信息了
select *
from tablename
where (name, birthday, areaID, mobile) in
(select name, birthday, areaID, mobile
from tablename
group by name, birthday, areaID, mobile
having count(*)>=2)
order by name

试试这个,速度有待反馈
select * from tb a
where exists
(select 1 from tb b
where a.name=b.name
and a.birthday=b.birthday
and a.areaid=b.areaid
and a.mobile=b.mobile
having count(*)>1
)

select a.name, a.bir