SQL

MySQL 접속 경과시간 Query

Link2Me 2024. 5. 1. 07:12
728x90

일정시간이 경과한 데이터는 삭제처리하는 로직을 만들기 위해서 구현해 본 쿼리다.

 

# 2시간 이내에 있는 데이터만 추출

select idx,userID,access_date from accessLog where access_date > subtime(current_timestamp(), '2:0:0') and access_date < current_timestamp();

 

# 2시간 경과한 데이터만 추출

select idx,userID,access_date from accessLog where access_date < subtime(current_timestamp(), '2:0:0');

 

# 2시간 경과한 데이터 삭제

delete from accessLog where access_date < subtime(current_timestamp(), '2:0:0');