How can I sum and then remove duplicate fields MySql?

限于喜欢 提交于 2021-02-08 07:56:11

问题


Sorry if this turns out to be duplicate but I am having a hard time finding the exact answer...

I have table that contains 'email'(varchar) and 'clicks'(int).

The email field contains duplicate instances of email values. I would like to remove the duplicates and keep just one instance but also sum all values found in 'clicks' for that given email instance and update the remaining record to reflect that.

I can use distinct and group by to get a view of the records I am after, its the sum and update part I am stumped with.

Thanks.


回答1:


I don't think you need to use DISTINCT here, but rather you can just group on each email value, taking the sum of clicks as you go along:

SELECT email, SUM(clicks) AS clickSum
FROM yourTable
GROUP BY email

When you mentioned "remaining" record, you neglected to mention how the duplicate records ever got deleted. It would be a bunch of work to selectively remove all but one duplicate record. I might go about this by just inserting the above query into a new table, deleting the old table, and renaming the new one to the old one:

CREATE TABLE yourNewTable (`email` varchar(255), `clicks` int);
INSERT INTO yourNewTable (`email`, `clicks`)
SELECT email, SUM(clicks)
FROM yourTable
GROUP BY email

DROP TABLE yourTable
ALTER TABLE yourNewTable RENAME TO yourTable



回答2:


Only update the email with more clicks.

UPDATE yourTable a 
JOIN (SELECT email, 
             MAX(clicks) as m_clicks, 
             SUM(clicks) as s_clicks
      FROM yourTable
      GROUP BY email) b
  ON a.email = b.email
 AND a.clicks = b.m_clicks
SET a.clicks = b.s_clicks;

Then DELETE

DELETE yourTable a
LEFT JOIN   yourTable b
       ON   a.clicks > b.clicks
WHERE b.clicks is NOT NULL

EDIT:

I just realize you can have two rows with same number of clicks. That is why you should always have one ID columns to work as PK.

So you can add to eliminate any duplicates.

ALTER IGNORE TABLE yourTable
ADD UNIQUE INDEX idx_clicks (email, clicks);



回答3:


You may use aggregate function sum() and group by.

SELECT email, sum(clicks) as sum FROM tableName group by email


来源:https://stackoverflow.com/questions/39022069/how-can-i-sum-and-then-remove-duplicate-fields-mysql

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!