如果记录的重复行具有最大值,请更新记录

时间:2016-08-20 07:33:08

标签: mysql sql duplicates distinct

我以这种方式将一个表中的唯一记录插入到另一个表中:

insert into unidata 
select distinct SourceFrom,Updated,Location_Type,First_Name,Middle_Name,
Last_Name,Designation,Company_Name,Address1,Address2,Address3,City,Pincode,STD_code,Extn,Fax_No,Office_Mobile_No,Website,Email_Office,Email_Personal1,Email_Personal2,
Residence_Address1,Residence_Address2,Residence_Address3,Residence_City,Residence_Pincode,Residence_STD_Code,
Residence_Phone_No,Personal_Mobile1,Personal_Mobile_NDNC,Work_Experience,Key_Skills,Personal_Mobile2,
State
from mytable src
where not exists(select *
             from unidata dest 
             where (src.SourceFrom=dest.SourceFrom and src.Updated=dest.Updated and src.Location_Type=dest.Location_Type
                    and src.Industry=dest.Industry and  src.First_Name=dest.First_Name and
                    src.Middle_Name=dest.Middle_Name and src.Last_Name=dest.Last_Name and src.Designation=dest.Designation and
                    src.Company_Name=dest.Company_Name and src.Address1=dest.Address1 and src.Address2=dest.Address2 and
                    src.Address3=dest.Address3 and src.City=dest.City and src.Pincode=dest.Pincode and src.STD_code=dest.STD_code and
                    src.Office_Mobile_No=dest.Office_Mobile_No and 
                    src.Website=dest.Website and src.Email_Office=dest.Email_Office and src.Email_Personal1=dest.Email_Personal1 and
                    src.Email_Personal2=dest.Email_Personal2 and src.Residence_Address1=dest.Residence_Address1 and
                    src.Residence_Address2=dest.Residence_Address2 and src.Residence_Address3=dest.Residence_Address3 and
                    src.Residence_City=dest.Residence_City and src.Residence_Pincode=dest.Residence_Pincode and
                    src.Residence_STD_Code=dest.Residence_STD_Code and src.Residence_Phone_No=dest.Residence_Phone_No and
                    src.Personal_Mobile1=dest.Personal_Mobile1 and src.Personal_Mobile_NDNC=dest.Personal_Mobile_NDNC and
                    src.Work_Experience=dest.Work_Experience and src.Key_Skills=dest.Key_Skills and
                   src.Personal_Mobile2=dest.Personal_Mobile2 and src.State=dest.State)
             ) 

这样它就会插入具有完全相同重复项的记录,就像具有完全相同的填充列的行一样。但是有些记录是重复的,但记录越来越少。

Email_Personal1 | Email_P2 |key_skills | state
abc@yahoo.com       null      null       null             
abc@yahoo.com       null      DBA        null 

上面是示例,我只出现了几列。它适用于所有列。就像很少的列在该行中具有相同的记录,并且其重复的行具有更多具有新值的列,我无法使用上述查询删除它。它采用了诸如唯一记录之类的东西。我想用其重复行中的所有可用列更新记录,然后删除删除重复行。我想这样:

Email_Personal1 | Email_P2 |key_skills | state
abc@yahoo.com       null      DBA        null   

只需将其存储为最终的唯一记录,而不是将两个记录保持为唯一。谁能在这里指导我?

更新(输出): enter image description here

0 个答案:

没有答案