删除 id 最大的地方 [英] delete where id is the biggest

查看:53
本文介绍了删除 id 最大的地方的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想删除具有最大 order_id 的行,我试过这个:

I would like to detele the row with the biggest order_id, I tried this:

DELETE FROM orders WHERE MAX(order_id)

但这是错误的!还有其他想法吗?

But this is wrong! any other ideas?

感谢您抽出宝贵时间.

推荐答案

第一个想法(在许多其他想法中完全相同):

First idea (among many others that had exactly the same):

DELETE FROM orders 
WHERE order_id = 
      ( SELECT MAX(order_id)
        FROM orders
      )

不幸的是 MySQL 抱怨:

Unfortunately MySQL complains with:

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

绕过错误的两种方法:

DELETE FROM orders 
WHERE order_id =
       ( SELECT maxo
         FROM  
           ( SELECT MAX(order_id) AS maxo
             FROM orders
           ) AS tmp
        )

或:

DELETE FROM orders
ORDER BY order_id DESC
LIMIT 1 

这篇关于删除 id 最大的地方的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆