Mysql
 sql >> Base de Dados >  >> RDS >> Mysql

delete onde id é o maior


Primeira ideia (entre muitas outras que tiveram exatamente o mesmo):
DELETE FROM orders 
WHERE order_id = 
      ( SELECT MAX(order_id)
        FROM orders
      )

Infelizmente o MySQL reclama com:
> ERROR 1093 (HY000): You can't specify target table 'orders' for update in FROM
> clause

Duas maneiras de contornar o erro:
DELETE FROM orders 
WHERE order_id =
       ( SELECT maxo
         FROM  
           ( SELECT MAX(order_id) AS maxo
             FROM orders
           ) AS tmp
        )

ou:
DELETE FROM orders
ORDER BY order_id DESC
LIMIT 1