Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
合并重复的电子邮件和点mysql_Mysql_Sql - Fatal编程技术网

合并重复的电子邮件和点mysql

合并重复的电子邮件和点mysql,mysql,sql,Mysql,Sql,我有一个MySQL表,它有三列: Userid | Email | Points --------------------------------------------------------- 1 | jdoe@company.com | 20 2 | jdoe%40company.com | 25 3 | rwhite@company.com | 14 4

我有一个MySQL表,它有三列:

Userid         | Email               | Points
---------------------------------------------------------
1              | jdoe@company.com    | 20
2              | jdoe%40company.com  | 25
3              | rwhite@company.com  | 14
4              | rwhite%40company.com| 10
我想做的是删除重复的电子邮件和合并点。我希望我的桌子看起来像这样:

Userid         | Email               | Points
---------------------------------------------------------
1              | jdoe@company.com    | 45
3              | rwhite@company.com  | 24
我的查询如何返回我的愿望表

有人知道怎么做吗


提前谢谢

你在找这样的东西吗

SELECT MIN(userid) userid, email, SUM(points) points
  FROM 
(
  SELECT userid, REPLACE(email, '%40', '@') email, points
    FROM table1
) q
  GROUP BY email
输出:

| USERID | EMAIL | POINTS | |--------|--------------------|--------| | 1 | jdoe@company.com | 45 | | 3 | rwhite@company.com | 24 | 这是演示

现在,如果要就地消除表中的重复数据,可以执行以下操作

-- Fix emails
UPDATE table1
   SET email = REPLACE(email, '%40', '@')
 WHERE email LIKE '%\%40%';
-- Merge points for duplicate records
UPDATE table1 t JOIN
(
  SELECT email, SUM(points) points
    FROM table1
   GROUP BY email
  HAVING COUNT(*) > 1
) q ON t.email = q.email
   SET t.points = q.points;
-- Delete all duplicate records except ones with lowest `userid`
DELETE t 
  FROM table1 t JOIN
(
  SELECT MIN(userid) userid, email
    FROM table1
   GROUP BY email
  HAVING COUNT(*) > 1
) q ON t.email = q.email
 WHERE t.userid <> q.userid;

这里是演示

假设您希望在不进行任何修改的情况下按原样匹配电子邮件,则使用此查询

SELECT MIN(user_id), SUM(points)as points, email FROM table_name GROUP BY email

@AndyMartin有帮助吗?