MySQL删除重复记录,但根据日期保留一个

时间:2018-03-02 01:17:44

标签: mysql

如何删除所有具有相同电子邮件地址的记录,但是按日期保留最新的记录? MySQL中的日期格式是" 0000-00-00"。我有以下查询:

delete from customer
 where date not in (
    select max(data)
      from customer
     group by email)

似乎不起作用。

你可以请你说明如何只保留一条记录,因为我的记录没有id

1 个答案:

答案 0 :(得分:-1)

加入一个子查询,获取每个客户的最新日期。

DELETE c1 FROM customer AS c1
JOIN (SELECT email, MAX(date) AS maxdate
      FROM customer
      GROUP BY email) AS c2
ON c1.email = c2.email AND c1.date != c2.maxdate