【问题标题】:How can I rephrase this UPDATE INNER JOIN query such that the columns of t1 are accessible in the subquery?我如何重新表述这个 UPDATE INNER JOIN 查询,以便在子查询中可以访问 t1 的列?
【发布时间】:2022-11-16 01:42:54
【问题描述】:

在运行以下 MySQL UPDATE 语句时,我收到错误“错误代码:1054。‘where 子句’中的未知列‘t1.col2’”.

如何改写此查询,以便在子查询中可以访问 t1 的列?

UPDATE MyFirstTable AS t1
INNER JOIN (
    SELECT col1, col2
    FROM MySecondTable
    WHERE col2 > t1.col2
) AS t2
ON t1.col1 = t2.col1
SET col3 = t1.col3;

【问题讨论】:

  • 不要使用子查询——它是多余的。为所有对象名称添加表别名。

标签: mysql sql sql-update inner-join


【解决方案1】:

您在这里不需要子查询,只需使用 multi-table update

像这样的东西应该工作:

UPDATE  MyFirstTable AS t1
JOIN    MySecondTable AS t2
ON      t1.col1 = t2.col1
SET     t2.col3 = t1.col3
WHERE   t2.col2 > t1.col2;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-09
    • 2014-02-24
    • 2013-12-22
    • 1970-01-01
    • 2013-08-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多