[英]delete where id is the biggest
I would like to detele the row with the biggest order_id, I tried this:我想删除 order_id 最大的行,我试过这个:
DELETE FROM orders WHERE MAX(order_id)
But this is wrong?但这是错的吗? any other ideas?
还有其他想法吗?
Thank you for your time.感谢您的时间。
First idea (among many others that had exactly the same):第一个想法(在许多其他完全相同的想法中):
DELETE FROM orders
WHERE order_id =
( SELECT MAX(order_id)
FROM orders
)
Unfortunately MySQL complains with:不幸的是 MySQL 抱怨:
> ERROR 1093 (HY000): You can't specify target table 'orders' for update in FROM
> clause
Two ways to bypass the error:绕过错误的两种方法:
DELETE FROM orders
WHERE order_id =
( SELECT maxo
FROM
( SELECT MAX(order_id) AS maxo
FROM orders
) AS tmp
)
or:或者:
DELETE FROM orders
ORDER BY order_id DESC
LIMIT 1
For MySQL, this would be easiest:对于 MySQL,这将是最简单的:
DELETE FROM orders
ORDER BY order_id DESC
LIMIT 1;
You can't do the subquery trick that several other people have answered, because MySQL doesn't like it if you select from a table and update/delete from it in the same query.您不能执行其他几个人已回答的子查询技巧,因为 MySQL 不喜欢如果您从表中 select 并在同一查询中更新/删除它。
you can try:你可以试试:
DELETE FROM orders where order_id = (select max(order_id) from orders)
try a subquery.尝试子查询。
DELETE FROM orders WHERE order_id = (SELECT MAX(order_id) FROM orders)
Try:尝试:
DELETE FROM orders where order_id = (SELECT MAX(order_id) FROM orders)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.