How do I delete either the first or last set of rows in a dynamic fashion

后端 未结 3 1115
太阳男子
太阳男子 2021-01-01 13:30

I would like to delete the first 100 rows or the last 100 rows in a certain table (ordered by the primary key).

Note: Lots of data is being spooled into this table.<

相关标签:
3条回答
  • 2021-01-01 14:20
    DELETE FROM table ORDER BY the field DESC|ASC limit 100
    
    0 讨论(0)
  • 2021-01-01 14:20
    SET @first = 1;
    delete from mytable 
    where primKey in (select 1 
                      from myTable 
                      order by 
                        CASE WHEN @first = 1 THEN primKey END ASC,
                        CASE WHEN @first <> 1 THEN primKey END DESC
                      limit 100)
    
    0 讨论(0)
  • 2021-01-01 14:24

    for first 100,

    DELETE FROM table ORDER BY <field> ASC limit 100
    

    and for last 100,

    DELETE FROM table ORDER BY <field> DESC limit 100
    
    0 讨论(0)
提交回复
热议问题