2016-05-16 118 views
1

我從表中刪除重複記錄的情況下,我沒有得到知道該怎麼做,因爲大數據和三代用表的關係這一點。刪除重複記錄,除了一個與加入三表

candidates_table是包含重複記錄候選表。 和字段

candidate Table : 

candidate_id |f_name |l_name| skills 

1    Ab  c  php,MySQL 
2    Ab  c  php,MySQL,java script 
3    cd  g  Java,hibernate,spring 
4    cd  g  Java,hibernate 
5    ef  h  XML,Web service 
6    ef  h  XML,Web service,json 

Attachment Table: 

attachment_id |candidate_id 

1    2   
2    4  
3    8  
4    9  
5    10  

Canidate_job_order Table: 

joborder_id |candidate_id 

1    2   
2    4  
3    8  
4    9  
5    10  

attachments_table是一個表,如果任何候選人有附件則candidate_id放在這裏。

candidate_joborder_table是包含如果如果candidate_id他對任何工作單被提交。

我不得不刪除候選表重複的候選人誰沒有附件,而不是針對任何作業提交order.Also我必須把1,如果一切都匹配相同。我想刪除候選表中的所有記錄,除了canidate_id 2,4,6任何幫助。

+3

這個問題不清楚!請提供更多詳細信息,一些示例數據和有關上次請求的更多信息。 – sagi

+0

好的我正在更新 – VipinS

+0

1.請參閱規範化 – Strawberry

回答

0

刪除沒有附件,也沒有提交作業訂單所有候選人。

DELETE * FROM candidate WHERE candidate_id NOT IN (
    SELECT C.candidate_id FROM candidate C 
    INNER JOIN Attachment A ON A.attachment_id = C.candidate_id 
    INNER JOIN Canidate_job_order CJO ON CJO.candidate_id = C.C.candidate_id 
) 

如果有的考生只有附件,這可能會刪除不使用外鍵的工作,如果你引用的表!

+0

重複記錄怎麼樣 – VipinS

+0

從您的描述來看,候選表中的重複記錄在_Attachment_和_Candidate_job_order_中沒有引用值,並且將被刪除。 –

+0

可能有記錄或不在參考表中。我們必須管理這兩個條件。 – VipinS

0

試試這個:

delete from candidate c where candidate_id not in (select max(candidate_id) 

from candidate 
group by f_name ,l_name, skills) 
and not exists (select 1 from Attachment A where A.attachment_id = C.candidate_id) 
and not exists (select 1 from Canidate_job_order CJO where CJO.candidate_id = C.C.candidate_id) and not in (2,4,6); 
+0

我們不會'不知道(2,4,6)中的id。關於重複記錄 – VipinS

+0

ID不在(2,4,6)中意味着「我想刪除候選表中除了canidate_id 2,4,6之外的所有記錄」 –

+0

更新了刪除重複的答案。通過選擇最大值查找不同的行。刪除其餘的。 –