我在MySQL 5.7.26中有一个表,我在那里存储产品的历史,我没有任何自动递增列。
+------------+---------------------+
| product_id | date_in |
+------------+---------------------+
| 500 | 2020-04-24 07:10:52 |
| 500 | 2020-04-24 07:12:11 |
| 500 | 2020-04-24 07:16:25 |
| 500 | 2020-04-24 07:18:32 |
| 500 | 2020-04-24 07:18:42 |
| 500 | 2020-04-24 13:51:30 |
| 500 | 2020-04-24 14:01:22 |
| 501 | 2020-04-24 07:10:52 |
| 501 | 2020-04-24 07:12:11 |
| 501 | 2020-04-24 07:16:25 |
| 501 | 2020-04-24 07:18:32 |
| 501 | 2020-04-24 07:18:42 |
| 501 | 2020-04-24 13:51:30 |
| 501 | 2020-04-24 14:01:22 |
+------------+---------------------+
我想只保留每个产品的最后2条历史记录,并删除所有的历史记录,所以它看起来像这样。
+------------+---------------------+
| product_id | date_in |
+------------+---------------------+
| 500 | 2020-04-24 13:51:30 |
| 500 | 2020-04-24 14:01:22 |
| 501 | 2020-04-24 13:51:30 |
| 501 | 2020-04-24 14:01:22 |
+------------+---------------------+
(后者需要MySQL 8.0)`。row_number()
在早期版本中,我们可以阐明
delete t
from mytable t
inner join (
select
product_id,
date_in,
row_number() over(partition by product_id order by date_in desc) rn
from mytable t
) t1
on t1.product_id = t.product_id
and t1.date_in = t.date_in
and t1.rn > 2
子查询 - 假设在 row_number()
:(product_id, date_in)
delete t
from mytable t
inner join (
select
product_id,
date_in,
(select count(*) from mytable t2 where t2.product_id = t1.product_id and t2.date_in >= t1.date_in) rn
from mytable t1
) t1
on t1.product_id = t.product_id
and t1.date_in = t.date_in
and t1.rn > 2;
DELETE p1
FROM
products p1
JOIN (
SELECT
product_id,
date_in
FROM
(
SELECT
p3.product_id,
p3.date_in,
( CASE @grouping WHEN p3.product_id THEN @rank := @rank + 1 ELSE @rank := 1 END ) AS rank,
@grouping := p3.product_id
FROM
products p3,
( SELECT @rank := 0, @grouping := 0 ) AS tt
ORDER BY
p3.product_id,
p3.date_in DESC
) tt2
WHERE
rank > 2
) p2 ON p1.product_id = p2.product_id
AND p1.date_in = p2.date_in