简体   繁体   English

在MySQL中按限制和条件删除记录

[英]Delete records by limit and condition in MySQL

I have a table named log in one of MySQL database. 我在MySQL数据库之一中有一个名为log的表。 And it is used to log the user actions. 它用于记录用户操作。 When it becomes very large in size, I want to delete some records. 当它变得很大时,我想删除一些记录。

SELECT `userid`, `timestamp` 
FROM `log`
ORDER `timestamp` ASC

the output is 输出是

userid    timestamp
     2    120000
     3    123333
     1    123456
     1    124444
     2    125555
     2    126666
     1    127777
     1    128888
     2    129999
     3    130000
     1    131111

What I want to do is that - I want to keep only last 3 records for each user. 我想做的是-我只想为每个用户保留最后3条记录 So, I need to delete the 4th, 5th, ..., nth records for each user. 因此,我需要为每个用户删除第4、5,...,n个记录。 According to the above sample, the desire output is 根据以上示例,期望输出为

userid    timestamp
     3    123333
     2    125555
     2    126666
     1    127777
     1    128888
     2    129999
     3    130000
     1    131111

I know that records can be deleted by using LIMIT . 我知道可以使用LIMIT删除记录。

 DELETE FROM `log` LIMIT 3

deletes only 3 records. 仅删除3条记录。 It simply cannot get my desire result. 它根本无法得到我想要的结果。

What I have tried is that 我试过的是

DELETE FROM
`log`
WHERE `userid` IN (
SELECT `userid` FROM (SELECT `userid`, COUNT(1) AS C
FROM `log`
GROUP BY `userid`
HAVING C > 3) CountTable ) LIMIT 3

And it is not what I wanted. 这不是我想要的。

Try this: 尝试这个:

DELETE l FROM `log` l 
LEFT JOIN (SELECT l.userid, l.timestamp
           FROM (SELECT l.userid, l.timestamp, 
                        IF(@lastUserId = @lastUserId:=userid, @Idx:=@Idx+1, @Idx:=0) rowNumber 
                 FROM `log` l, (SELECT @lastUserId:=0, @Idx:=0) A
                 ORDER BY l.userid, l.timestamp DESC
                ) AS A
           WHERE rowNumber < 3
          ) AS A ON l.userid = A.userid AND l.timestamp = A.timestamp 
WHERE A.userid IS NULL

EDIT: 编辑:

DELETE l FROM `log` l 
WHERE NOT EXISTS (
          SELECT 1
          FROM (SELECT l.userid, l.timestamp, 
                       IF(@lastUserId = @lastUserId:=userid, @Idx:=@Idx+1, @Idx:=0) rowNumber 
                FROM `log` l, (SELECT @lastUserId:=0, @Idx:=0) A
                ORDER BY l.userid, l.timestamp DESC
               ) AS A
          WHERE l.userid = A.userid AND l.timestamp = A.timestamp AND rowNumber < 3
         )

Check the SQL FIDDLE DEMO 检查SQL FIDDLE DEMO

OUTPUT OUTPUT

| USERID | TIMESTAMP |
|--------|-----------|
|      3 |    123333 |
|      2 |    125555 |
|      2 |    126666 |
|      1 |    127777 |
|      1 |    128888 |
|      2 |    129999 |
|      3 |    130000 |
|      1 |    131111 |

Try below SQL: 请尝试以下SQL:

DELETE FROM log WHERE find_in_set(
    TIMESTAMP, (
        SELECT group_concat(t3) t4 FROM (
            SELECT 1 AS dummy,
            replace(group_concat(TIMESTAMP ORDER BY TIMESTAMP DESC), concat(SUBSTRING_INDEX(group_concat(TIMESTAMP ORDER BY TIMESTAMP DESC), ',', 3), ','), '') t3
            FROM log
            GROUP BY user_id HAVING count(*) > 3
        ) a GROUP BY dummy
    )
)

SQL Fiddle SQL小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM