删除表中的所有行,但不删除最新的X with子句

时间:2013-02-11 17:58:15

标签: mysql sql

我有我的表transfer

| id  | fix | part |       created       |
|  +  |  +  |   +  |          +          |
|  +  |  +  |   +  |          +          |
| 238 |  1  |   1  | 2012-02-10 21:15:48 |
| 239 |  9  |   1  | 2012-02-11 12:36:17 |
| 240 |  1  |   2  | 2012-02-12 23:35:28 |
| 241 |  2  |   1  | 2012-02-13 06:17:35 |
| 242 |  4  |   2  | 2012-02-14 17:45:42 |
| 243 |  1  |   1  | 2012-02-15 20:32:58 |
| 244 |  2  |   2  | 2012-02-16 12:52:19 |
| 245 |  3  |   1  | 2012-02-17 22:35:56 |
| 246 |  1  |   2  | 2012-02-18 09:11:23 |
| 247 |  3  |   1  | 2012-02-19 19:46:44 |
| 248 |  1  |   1  | 2012-02-20 02:30:14 |
| 249 |  2  |   1  | 2012-02-21 13:36:49 |
| 250 |  1  |   3  | 2012-02-22 21:35:34 |
| 251 |  1  |   1  | 2012-02-23 19:25:12 |
| 252 |  1  |   2  | 2012-02-24 18:53:43 |
| 253 |  1  |   3  | 2012-02-25 21:05:28 |
| 254 |  3  |   1  | 2012-02-26 12:33:35 |
| 255 |  1  |   1  | 2012-02-27 18:35:18 |
| 256 |  4  |   1  | 2012-02-28 22:15:27 |
| 257 |  4  |   1  | 2012-03-01 12:22:17 |
| 258 |  2  |   2  | 2012-03-02 10:19:24 |
| 259 |  9  |   1  | 2012-03-03 18:45:46 |
| 260 |  1  |   2  | 2012-03-04 23:19:07 |
| 261 |  2  |   1  | 2012-03-05 09:11:11 |
| 262 |  1  |   1  | 2012-03-06 21:25:29 |
|  +  |  +  |   +  |          +          |
|  +  |  +  |   +  |          +          |
| 901 |  1  |   3  | 2012-04-30 22:15:27 |
| 902 |  3  |   1  | 2012-05-01 12:22:17 |
| 903 |  2  |   1  | 2012-05-02 10:19:24 |
| 904 |  1  |   1  | 2012-05-03 18:45:46 |
|  +  |  +  |   +  |          +          |
|  +  |  +  |   +  |          +          |

并希望保留每个相同createdfix列的最新(part)3行,间隔为...,例如3个月。如果只有1或2行显示它们! (例如,参见id 242)

我尝试了一些事情并在stackoverflow上搜索,但是我没有找到额外列(part)的解决方案。

transfer在处理后应如下所示:

| id  | fix | part |       created       |
| 904 |  1  |   1  | 2012-05-03 18:45:46 |
| 262 |  1  |   1  | 2012-03-06 21:25:29 |
| 255 |  1  |   1  | 2012-02-27 18:35:18 |
| 260 |  1  |   2  | 2012-03-04 23:19:07 |
| 252 |  1  |   2  | 2012-02-24 18:53:43 |
| 246 |  1  |   2  | 2012-02-18 09:11:23 |
| 901 |  1  |   3  | 2012-04-30 22:15:27 |
| 253 |  1  |   3  | 2012-02-25 21:05:28 |
| 250 |  1  |   3  | 2012-02-22 21:35:34 |
| 903 |  2  |   1  | 2012-05-02 10:19:24 |
| 261 |  2  |   1  | 2012-03-05 09:11:11 |
| 249 |  2  |   1  | 2012-02-21 13:36:49 |
| 258 |  2  |   2  | 2012-03-02 10:19:24 |
| 244 |  2  |   2  | 2012-02-16 12:52:19 |
| 902 |  3  |   1  | 2012-05-01 12:22:17 |
| 254 |  3  |   1  | 2012-02-26 12:33:35 |
| 247 |  3  |   1  | 2012-02-19 19:46:44 |
| 257 |  4  |   1  | 2012-03-01 12:22:17 |
| 256 |  4  |   1  | 2012-02-28 22:15:27 |
| 242 |  4  |   2  | 2012-02-14 17:45:42 |
| 259 |  9  |   1  | 2012-03-03 18:45:46 |
| 239 |  9  |   1  | 2012-02-11 12:36:17 |

我按fixpart订购了示例,以便更好地理解。

也许有人可以给我一个提示?

2 个答案:

答案 0 :(得分:1)

您可以尝试这样的事情:

delete from transfer
where id not in (
  select id
  from (
    select id, fix, part, created,
    (select count(*) from transfer where created >= a.created and fix = a.fix and part = a.part) as rank
    from transfer a
  ) as t
  where rank <= 3
);

我正在使用相关子查询对行进行排名,这将具有triangle join的固有性能缺陷(SQL Server文章,但stil适用)。

这是 SqlFiddle demo

答案 1 :(得分:1)

您可以使用以下命令获取要删除的ID列表:

select t.*,
       group_concat(id separator ',' order by created desc) as ids
from transfer
where created >= curdate - interval 3 month
group by fix, part

您可以将其转换为删除:

delete from t
    where not exists (select 1
                  from (select fix, part,
                               group_concat(id separator ',' order by created desc) as ids
                        from transfer
                        where created >= curdate - interval 3 month
                        group by fix, part
                       ) t1
                  where t.id not in (substring_index(ids, 1), substring_index(ids, 2), substring_index(ids, 3))
                 )

虽然MySQL对于在子查询中使用同一个表进行删除非常繁琐,但它确实允许它使用多级子查询。