2

我有一些 MySQL 表,其中包含需要删除的冗余数据。例如:

 id email            date       data...
 1  email1@gmail.com 2012-01-01 my_data
 2  email2@gmail.com 2012-01-01 my_data
 3  email1@gmail.com 2012-01-02 my_data
 4  email1@gmail.com 2012-01-02 my_data   (redundant)
 5  email2@gmail.com 2012-01-02 my_data

我需要删除多余的行,但我想先选择它们。我在 StackOverflow 上找到了这个,但它需要电子邮件地址

SELECT * 
FROM `my_table`
WHERE `id` IN (SELECT `id` 
               FROM `my_table` 
               where `email` = 'email1@gmail.com' 
               group by `date` 
               HAVING count(*) > 1)

我可以像上面那样使用什么查询,在嵌入式查询中不使用 WHERE 限定符,所以我可以在所有电子邮件地址中使用它?

查询可以是 SELECT 查询。我不介意在 PHPMyAdmin 中手动删除行。

4

2 回答 2

7
DELETE FROM tableName
WHERE ID NOT IN
(
    SELECT minID
    FROM
    (
        SELECT email, date, MIN(id) minID
        FROM tableNAme
        GROUP BY email, date
    ) x
)

或通过使用JOIN

DELETE a 
FROM tableName a
    LEFT JOIN (
            SELECT minID
            FROM (
                    SELECT email, DATE, MIN(id) minID
                    FROM tableNAme
                    GROUP BY email, DATE
                    ) y
            ) x
            ON a.ID = x.minID
WHERE x.minID IS NULL;

以下查询仅SELECT重复每个电子邮件日期的行

SELECT a.*
FROM tableName a
        LEFT JOIN 
       ( 
         SELECT minID
        FROM
        (
          SELECT email, date, MIN(id) minID
          FROM tableNAme
          GROUP BY email, date
        )y
       ) x ON a.ID = x.minID
WHERE x.minID IS NULL
于 2012-10-28T07:11:12.020 回答
0

另一种方法是计算表中每个电子邮件地址的日期列的出现次数:

SELECT `email`, `date`, COUNT(*) FROM `my_table` GROUP BY `date`, `email` HAVING COUNT(*) > 1

+------------------+---------------------+----------+
| email            | date                | COUNT(*) |
+------------------+---------------------+----------+
| email1@gmail.com | 2012-01-02 00:00:00 |        2 |
+------------------+---------------------+----------+
于 2012-10-28T07:50:25.777 回答