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

后端 未结 3 1134
太阳男子
太阳男子 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

    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)
    

提交回复
热议问题