1

3 つの列を持つ 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

クエリが自分の欲求テーブルを返すのはどのように見えるでしょうか?

誰でもこれを行う方法を知っていますか?

前もって感謝します!

4

2 に答える 2

2

このようなものをお探しですか?

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

出力:

| | ユーザーID | 電子メール | ポイント |
|--------|--------------------|--------|
| | 1 | jdoe@company.com | 45 |
| | 3 | rwhite@company.com | 24 |

これがSQLFiddleのデモです


テーブルをインプレースで重複排除したい場合は、次のことができます

-- 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;

これがSQLFiddleのデモです

于 2013-08-29T05:55:03.813 に答える
0

電子メールを変更せずにそのまま照合する場合は、このクエリを使用します

SELECT MIN(user_id), SUM(points)as points, email FROM table_name GROUP BY email
于 2013-08-29T06:01:02.867 に答える