2016-09-21 83 views
2

我正在嘗試查找潛在的重複記錄。例如。 凡如何從4列中有3列具有相同值的表中選擇?

  • 名字=姓
  • 姓氏=姓氏
  • DOB = DOB
  • 街道地址=街道地址

所以名字,姓氏和出生日期都是一樣的。或FirstName,LastName和StreetAddress。或LastName,DOB和SteetAddress。依此類推......

我的客戶想要4個值中有3個相同的記錄。 這可以在MySql中完成嗎?或者我需要在PHP中進行一些處理?

回答

0

客戶(ID,姓名,出生日期)

要檢索具有相同名字的客戶ID:

select id 
from customer 
where firstname in (select firstname 
      from customer 
      group by firstname 
      having count(*)>1); 

否則你就必須在自身加入表:

select a.id 
from customer a, customer b 
where (a.firstname=b.firstname and a.lastname=b.lastname and a.dob=b.dob) or 
     (a.firstname=b.firstname and a.lastname=b.lastname and a.address=b.address) or 
     (a.firstname=b.firstname and a.dob=b.dob and a.address=b.address) or 
     (a.lastname=b.lastname and a.dob=b.dob and a.address=b.address); 
+0

這隻給我FirstName是一個匹配。我需要匹配4箇中的3個 – David

1

只需使用SELECT DISTINCT:

SELECT DISTINCT first, last, dob, street 
FROM Your_Table 
0

首先添加索引:

alter table whatever add key(DOB,LastName,FirstName,StreetAddress); 

然後看在三個字段,如果你重複的行 - 在這種情況下DOB,FN和LN:

select DOB,LastName,FirstName,StreetAddress from whatever t1 
inner join (
select DOB,LastName,FirstName,StreetAddress from whatever 
group by DOB,LastName,FirstName,StreetAddress) as t2 
on t1.DOB=t2.DOB 
and t1.LastName=t2.LastName 
and t1.FirstName=t2.FirstName 
where t1.StreetAddress != t2.StreetAddress; 

尋找其他組合,改變你加入的領域和在哪裏條款的字段..

相關問題