gpt4 book ai didi

php - MySQL 中基于另一个字段的最大值更新字段的嵌套 select 语句给出错误

转载 作者:行者123 更新时间:2023-11-29 13:21:28 25 4
gpt4 key购买 nike

我想在PhP中执行以下SQL语句。我在下面简化了它。我想知道 MySQL 的修改使其工作。我尝试了一些方法,例如为表提供别名,但它不起作用:

     update T1 set col1 = 500 
where col2 = 12345 and
col3 = (select max(col3) from T1 where col2 = 12345)

我想更新一条具有一列最大值的记录。整个查询只涉及一张表。我正在使用 PDO(如果相关的话)。

给出的错误是:

#1093 - You can't specify target table 'T1' for update in FROM clause 

最佳答案

改用join:

 update T1 join
(select max(col3) as maxcol3
from T1 t11
where col2 = 12345
) tmax
on T1.col3 = tmax.maxcol3 and
T1.col2 = 12345
set T1.col1 = 500;

关于php - MySQL 中基于另一个字段的最大值更新字段的嵌套 select 语句给出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20788905/

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