Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 删除表格中的所有行,但不删除最新的X with子句_Mysql_Sql - Fatal编程技术网

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

Mysql 删除表格中的所有行,但不删除最新的X with子句,mysql,sql,Mysql,Sql,我要转台 并希望保留最新创建的3行,每个相同的修复和部分列的间隔为。。例如3个月。如果只有1或2行,则显示它们!例如,参见ID242 我尝试了一些方法并在stackoverflow上进行了搜索,但我没有找到包含额外列部分的解决方案 表传输在处理后应如下所示: | id | fix | part | created | | 904 | 1 | 1 | 2012-05-03 18:45:46 | | 262 | 1 | 1 | 2012-03-06 21

我要转台

并希望保留最新创建的3行,每个相同的修复和部分列的间隔为。。例如3个月。如果只有1或2行,则显示它们!例如,参见ID242

我尝试了一些方法并在stackoverflow上进行了搜索,但我没有找到包含额外列部分的解决方案

表传输在处理后应如下所示:

| 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顺序排列了这个示例


也许有人能给我一个提示

您可以尝试以下方法:

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文章固有的性能缺陷,但仍然适用


这是一个。

您可以使用以下方法获取要删除的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对在子查询中使用同一个表进行删除非常挑剔,但它确实允许使用多个级别的子查询。

可以相信id是按升序排列的吗?可以创建一个连接吗?@TimLehner不明白你的意思:但是“应该看起来”示例中的id与表id、fix、part、created上面的形式完全相同。只是修订版1 |第1部分、修订版1 |第2部分、修订版1 |第3部分中最新的3个,因此未更新以删除,尽管我不确定我是否理解您的间隔要求。您是对的。。我想我不需要休息。我可以通过将cronjob设置为time X来处理时间间隔
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))
                 )