从表中删除旧记录

时间:2012-12-10 06:43:21

标签: mysql sql

我正在尝试从不同的卖家导入产品数据,数据会导入到包含以下字段的导入表中: id =自动增量 卖方=卖方的关键 sellerproductid =卖方给产品的产品ID lastupdate =此项目更新的日期

因此,卖家A可以拥有产品ID 22,该产品与产品ID为55的卖家B相同。

当卖家A更改产品A的价格时,导入中会有一行。我目前有许多“历史”线,我只对获得最新的导入感兴趣

例如:

1  A   22     2012 01 22
2  A   22     2012 05 10
44 A   22     2012 11 10

我想将前两行归档如下:

insert into archive ....
values ( select * from import where id in(
 select id from import 
 where `lastupdate` < max(`lastupdate`)
 group by sellerid, sellerproductid
)

我无法找到合适的mysql语句来获取旧记录的ID。

1 个答案:

答案 0 :(得分:2)

除了最新的条目,这应该给出所有条目:

SELECT distinct t1.* 
FROM import t1 inner join import t2 
on t1.sellerid = t2.sellerid and t1.sellerproductid = t2.sellerproductid 
where t1.lastupdate < t2.lastupdate;
相关问题