1

我有我的桌子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 |
|  +  |  +  |   +  |          +          |
|  +  |  +  |   +  |          +          |

并希望保持最新的 ( created) 3 行的每一个相同的fixpart,间隔为.. 例如 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 |

为了更好地理解fix,我订购了这个例子。part

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

4

2 回答 2

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
);

我正在使用相关子查询对行进行排名,这将具有三角形连接的固有性能缺陷(SQL Server 文章,但仍然适用)。

这是一个SqlFiddle 演示

于 2013-02-11T18:15:55.530 回答
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 对在子查询中使用同一张表进行删除很挑剔,但它确实允许使用多个级别的子查询。

于 2013-02-11T18:22:07.757 回答