gpt4 book ai didi

mysql - SQL删除时间戳早于(现在-x天)的所有行,但保留最近的n条记录

转载 作者:行者123 更新时间:2023-11-29 01:55:12 25 4
gpt4 key购买 nike

如果我有下表,我需要删除所有早于(现在 - x 天)但不在最近 n 条记录中的记录。

具体示例:用户在 90 天内不能重复使用密码并且不得重复使用最后 10 个密码。如果我每 90 天更改一次密码,我仍然不能重复使用密码 10 次更改。

CREATE TABLE PASSWORD_HISTORY (
ID BIGINT NOT NULL AUTO_INCREMENT,
USER_NAME VARCHAR(255) NOT NULL,
PASSWORD VARCHAR(255) NOT NULL,
SALT VARCHAR(255),
CREATED_TIMESTAMP BIGINT NOT NULL,
UPDATED_TIMESTAMP BIGINT NOT NULL,
TENANT_ID INTEGER DEFAULT -1234,
PRIMARY KEY (ID)
)ENGINE INNODB;

最佳答案

-- drop table password_history;
create table password_history
(
id bigint null auto_increment primary key,
user_name varchar(255) not null,
created_timestamp bigint not null
);

-- delete from password_history where id>0; -- safe mode sometimes barfs
insert password_history (user_name,created_timestamp) values ('fred',100);
insert password_history (user_name,created_timestamp) values ('fred',200);
insert password_history (user_name,created_timestamp) values ('fred',300);
insert password_history (user_name,created_timestamp) values ('fred',400);
insert password_history (user_name,created_timestamp) values ('fred',401);
insert password_history (user_name,created_timestamp) values ('fred',402);
insert password_history (user_name,created_timestamp) values ('fred',403);
insert password_history (user_name,created_timestamp) values ('fred',404);
insert password_history (user_name,created_timestamp) values ('fred',405);
insert password_history (user_name,created_timestamp) values ('fred',406);
insert password_history (user_name,created_timestamp) values ('fred',407);
insert password_history (user_name,created_timestamp) values ('fred',500);
insert password_history (user_name,created_timestamp) values ('fred',555);

insert password_history (user_name,created_timestamp) values ('fred',unix_timestamp(now()) );
insert password_history (user_name,created_timestamp) values ('stan',unix_timestamp(now()) );

alter table password_history add deleteMe int;

select * from password_history;

-- variables n and d
-- n=10, users last 10 records
-- d=90, last 90 days
-- rows where password created more than 90 days ago (replace 90 below as desired)
select * from password_history
where unix_timestamp(now()) - created_timestamp>(60*60*24*90)
order by id desc

-- update password_history set deleteMe=1 where id>0; -- safe mode sometimes barfs

-- update password_history set deleteMe=null where id>0; -- safe mode sometimes barfs

update password_history
join
(
select ph.id,ph.user_name,ph.created_timestamp
from password_history ph
join
(
select ph.id,ph.user_name,ph.created_timestamp
from password_history ph
join
(
select user_name,max(id),max(created_timestamp),count(*) as theCount
from password_history xx
group by user_name
having theCount>10
) inR2
on ph.user_name=inR2.user_name
order by ph.user_name,ph.created_timestamp desc
limit 10
) inR1
on ph.id=inR1.id
) bigThing
on password_history.id=bigThing.id
set deleteMe='1'
where password_history.id>0 -- this gets rid of the safe mode barfing

update password_history
join
(
select user_name from password_history p2 where p2.deleteMe=1
) phMany
on password_history.user_name=phMany.user_name
set deleteMe=2
where password_history.deleteMe is null

-- select * from password_history order by user_name,created_timestamp desc;

-- look at the ones with deleteMe=2

关于mysql - SQL删除时间戳早于(现在-x天)的所有行,但保留最近的n条记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30960368/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com