Delete oldest records from database

前端 未结 3 534
悲哀的现实
悲哀的现实 2020-12-23 21:01

I have a database with 1000 records. I am trying to create an SQL statement so if the number of records grows above 1000, then the oldest records are deleted (i.e. the new r

3条回答
  •  情话喂你
    2020-12-23 21:57

    For delete all records except the first record (min/max id) you can use:

    SET @ls AS INT
    
    SELECT @ls = MIN(id) FROM DATA
    
    DELETE FROM DATA WHERE id <> @ls
    

提交回复
热议问题