Home > database >  Ask god how to write a delete history of SQL statements
Ask god how to write a delete history of SQL statements

Time:10-16

I have a table table_user_login_history, save the user's login history, but I just want to save each user login record in recent one month, so set up a regular task, to do this every other month, but how to define inside to write SQL statements to delete the earlier records, only keep each user login record in recent one month, thank you.

CodePudding user response:

The DELETE FROM table_user_login_history WHERE CREATE_DATE & lt; DATE_SUB (CURDATE (), the INTERVAL 1 MONTH);

CodePudding user response:

Like the wrong, is keeping every user in recent 100 records,

CodePudding user response:

refer to the second floor small ambition dad reply:
, like wrong, is retained each user 100 records recently,

Don't know this can meet your request, I have no data validation
 
The delete t * from
The test t,
(select MIN (create_time) as create_time from test where user_id=@ user_id order by create_time desc limit 100) b
Where tc reate_time & lt; The biggest reate_time and t.u ser_id=@ user_id
  • Related