gpt4 book ai didi

MySQL : Updating a table having a 'where' clause with a max value

转载 作者:太空宇宙 更新时间:2023-11-03 11:06:17 25 4
gpt4 key购买 nike

我想更新最后生成的行(max(id) 行。

我试过这段代码,但它不起作用

update person t1
set t1.age = 25
where t1.id = (select max(t2.id) from person t2
where t2.address = 'LA, California');

MySQL 告诉我:错误代码:1093。您不能在 FROM 子句中指定要更新的目标表 't1'

因此,我想我无法在执行更新等操作时得出相同的故事。

我该如何解决这个问题?

问候。

最佳答案

你可以这样尝试:

UPDATE person t1
INNER JOIN (SELECT MAX(id) AS id FROM person
WHERE t2.address = 'LA, California') t2
ON t1.id = t2.id
SET t1.age = 25;

SELECT MAX(t2.id)
INTO @var_max_id
FROM person t2
WHERE t2.address = 'LA, California';

UPDATE person t1
SET t1.age = 25
WHERE t1.id = IFNULL(@var_max_id, -1);

关于MySQL : Updating a table having a 'where' clause with a max value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11719346/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com