How to select all records from table apart from the last 100

前端 未结 2 1213
独厮守ぢ
独厮守ぢ 2021-01-27 12:07

I have a database that stores customers records and I would like to set up a cron job to overwrite these records periodically. I would like to say Select * from ORDERS where ORD

相关标签:
2条回答
  • 2021-01-27 12:51

    You can left join a rowset of 100 last order_id's - this will result in all but 100 last having NULL in the left joined set.

    SELECT o.* from `order-table` o
    LEFT JOIN
      ( SELECT order_id FROM `order-table` ORDER BY order_id DESC LIMIT 100 ) o100
    ON o.order_id = o100.order_id
    WHERE o100.order_id IS NULL
    
    0 讨论(0)
  • 2021-01-27 12:52
    select *
    from Orders
    where OrderID not in (
        select OrderID 
        from Orders 
        order by OrderID desc 
        limit 100)
    
    0 讨论(0)
提交回复
热议问题