Mysql根据列值删除重复的行,但保留最新的行

时间:2016-08-27 09:36:36

标签: mysql

我有一个包含3个字段的表(phonecall,calldate,checked),我想通过phonecall列值删除重复的行,但不是最新的。

enter image description here

在这种情况下,我想从该表的第一行中删除2行。

由于

2 个答案:

答案 0 :(得分:1)

像这样:

delete A from TableX A
  left join TableX B
    on B.CallDate>A.CallDate and B.PhoneCall=A.PhoneCall
 where B.PhoneCall is not null

答案 1 :(得分:0)

这可能有效:

delete callLog
from callLog
  inner join (
     select max(CallDate) as lastCallDate, PhoneCall
     from callLog
     group by PhoneCall
     having count(*) > 1) duplic on duplic.PhoneCall = callLog.PhoneCall
where callLog.CallDate < duplic.lastCallDate;