合并重复记录,根据MySql

时间:2018-02-28 15:27:17

标签: mysql duplicates

我有一个特殊的欺骗问题。我可以很容易地识别记录,但我需要做的基本上是对一些附带数据的合并。

这就是问题所在。我的表有点像这样:

CREATE TABLE `People` (
  `PersonId` int(11) NOT NULL AUTO_INCREMENT,
  `Address` varchar(255) DEFAULT NULL,
  `Title` varchar(50) DEFAULT NULL,
  `Forename` varchar(150) DEFAULT NULL,
  `Surname` varchar(150) DEFAULT NULL,
  `FlagOne` bit(1) NOT NULL DEFAULT b'0',
  `FlagTwo` bit(1) NOT NULL DEFAULT b'0',
  `FlagThree` bit(1) NOT NULL DEFAULT b'0',
  PRIMARY KEY (`PersonId`)
)

重复记录仅在标题和标记值上有所不同 - 它们通过具有相同的地址,姓名和姓氏字段来标识为重复记录:

PersonId    Address         Title   Forename    Surname FlagOne FlagTwo FlagThree
1           6 Smith Street  Mrs     Jane        Doe     1       0       0
2           6 Smith Street  Ms      Jane        Doe     0       1       0

我无法弄清楚如何将这两个合并为一个记录保留所有正面标记的记录。保留两个原始记录中的哪一个并不重要 - 使用PersonId区分它们很好。所以,这样的事情是理想的结果:

PersonId    Address         Title   Forename    Surname FlagOne FlagTwo FlagThree
2           6 Smith Street  Ms      Jane        Doe     1       1       0

我知道如何基于联接进行更新,但我不确定如何实现获得此特定结果所需的条件?

2 个答案:

答案 0 :(得分:3)

你说你知道如何使用JOIN进行更新,所以这样的事情会给出合并:

 SELECT MAX(PersonId),
        Address,
        MAX(Title), 
        Forename,
        Surname
        MAX( FlagOne ),
        MAX( FlagTwo ), 
        MAX( FlagThree)
FROM People
GROUP BY Address,
         Forename,
         Surname

然后你需要删除重复项

DELETE People
WHERE PersonID IN (SELECT MIN(PersonId)
                   FROM People
                   GROUP BY Address,
                            Forename,
                            Surname
                   HAVING COUNT(*) > 1)

这假设只有重复,如果你有三个具有相同Forename,Surname的行。需要一个不同的方法。

答案 1 :(得分:1)

我认为你需要分两步完成:

1-更新值:

  constructor(private router: Router, private activatedRoute: ActivatedRoute)

  status:number
  ngOnInit()
  {
      if (this.router.url.indexOf("create")>=0)
      {
          status=0;
      }
      else
      {
          this.activatedRoute.paramMap.subscribe((params: ParamMap) =>
          { 
              console.log(params.get('id'))
              if (this.router.url.indexOf("view")>=0)
                 status=1;
              if (this.router.url.indexOf("edit")>=0)
                status=2;
          })
      }
  }

2-删除:

Update People p
LEFT JOIN (
      SELECT MAX(PersonId) as PId,
            Address,
            MAX(Title) as title, 
            Forename,
            Surname
            MAX( FlagOne ) as FlagOne,
            MAX( FlagTwo ) as FlagTwo, 
            MAX( FlagThree) as Flagthree
    FROM People
    GROUP BY Address,
             Forename,
             Surname ) t

 ON t.Address = p.Address
AND t.Forename = p.Forename
AND t.Surname = p.Surname
SET p.FlagOne = t.FlagOne , 
    p.FlagTwo = t.FlagTwo , 
    p.FlagThree= t.FlagThree
WHERE p.PersonId = t.PId