【问题标题】:UPDATE temporary table using subquery and joined table fails使用子查询和连接表更新临时表失败
【发布时间】:2011-09-02 08:26:25
【问题描述】:

我的数据库中有一个评估线表,我想从第一次评估中获取一些数据,并将其与最新评估中的相同数据一起显示,以便轻松查看从第一次评估到当前评估的进展。我正在使用来自两个评估的三个值以及来自当前评估的其他数据来执行此操作。

为此,我认为我需要创建临时表来保存两组字段以及当前评估中的其他字段。

我的临时表创建如下:

CREATE TEMPORARY TABLE appraisal_lines_submitted (
    current_perceived_state INT(2),
    current_actual_state INT(2),
    current_desired_state INT(2),
    agreed_action_points TEXT,
    agreed_actions_target_date DATE,
    assessor_notes TEXT,
    category_id INT(2),
    sub_categories_id INT(2),
    sub_categories_description VARCHAR(45),
    sequence_number INT(3) PRIMARY KEY,
    first_perceived_state INT(2),
    first_actual_state INT(2),
    first_desired_state INT(2)
);

然后我运行如下插入和选择语句,将当前评估中的数据放入临时表中:

INSERT INTO appraisal_lines_submitted (
    current_perceived_state,
    current_actual_state,
    current_desired_state,
    agreed_action_points,
    agreed_actions_target_date,
    assessor_notes,
    category_id,
    sub_categories_id,
    sub_categories_description,
    sequence_number)
SELECT appraisal_lines.perceived_state,
    appraisal_lines.actual_state,
    appraisal_lines.desired_state,
    appraisal_lines.agreed_action_points,
    DATE_FORMAT (appraisal_lines.agreed_actions_target_date, '%d/%m/%Y') AS agreed_actions_target_date_formatted,
    appraisal_lines.assessor_notes,
    appraisal_lines.assessment_category_id,
    assessment_sub_categories.id,
    assessment_sub_categories.description,
    assessment_sub_categories.sequence_number
FROM appraisal_lines LEFT JOIN assessment_sub_categories
    ON appraisal_lines.assessment_sub_category_id = assessment_sub_categories.id
WHERE hyperlink_token ='db678f8595edcd78d8ea7f055f7ee790b804c91e'
    AND assessment_category_id ='5'
ORDER BY sequence_number;

这工作正常。

但是当我运行下面的更新语句时它失败了

UPDATE appraisal_lines_submitted JOIN
    (SELECT appraisal_lines.perceived_state as update_perceived_state
     FROM appraisal_lines
     WHERE hyperlink_token ='d7cc7e1adc116e0dac31cbad34cd9a2b322c3507'
        AND assessment_category_id ='5'
     ORDER BY appraisal_lines.assessment_sub_category_id
    ) AS increments 
    ON increments.update_perceived_state = appraisal_lines.perceived_state
    SET appraisal_lines_submitted.first_perceived_state = update_perceived_state;

出现错误

错误代码:1054
“on 子句”中的未知列“appraisal_lines.perceived_state”

我根据帖子写了更新声明:MySQL Update a field value with subquery with multiple returning rows

【问题讨论】:

    标签: mysql sql join sql-update mysql-error-1054


    【解决方案1】:

    这是您的脚本,其中一些以粗体突出显示:

    UPDATE appraisal_lines_submitted JOIN
        (SELECT appraisal_lines.perceived_state as update_perceived_state
         FROM appraisal_lines
         WHERE hyperlink_token ='d7cc7e1adc116e0dac31cbad34cd9a2b322c3507'
            AND assessment_category_id ='5'
         ORDER BY appraisal_lines.assessment_sub_category_id
        ) AS increments 
        ON increments.update_perceived_state = appraisal_lines.perceived_state
        SET appraisal_lines_submitted.first_perceived_state = update_perceived_state;
    

    现在,在您的脚本中,您将一个名为 appraisal_lines_submitted 的表连接到一个名为 increments 的(派生)表。连接条件引用来自increments 的列和来自...的列……等一下,appraisal_lines?那是什么?参与加入的人中没有同名的表。

    我的猜测是,加入条件中的appraisal_lines.perceived_state 引用应该替换为appraisal_lines_submitted.current_perceived_state

    我的另一个猜测是,您在ORDER BY 之后错过了LIMIT 1,这是基于正在更新的列的名称 (<b>first</b>_perceived_state)

    【讨论】:

    • 谢谢 Andriy 看起来它是我删除的 = 评估线.perceived_state 但现在临时表中的每一行都使用 SELECT 语句的第一行的结果进行了更新。任何帮助表示赞赏。
    • @Mike:恐怕我帮不了你。好吧,直到你详细说明它应该是怎样的。
    • 感谢 Tony 提出的重新关联子查询的建议,我能够让它工作,感谢您的帮助。
    【解决方案2】:

    解析器看不到表appraisal_lines,因为您正在更新appraisal_lines_submitted

    您在内部SELECT 中指定appraisal_lines 的唯一位置,别名为increments

    您也可以将ORDER BY 放在内部SELECT 中,无论内部数据的顺序如何,都会执行连接。

    【讨论】:

    • 感谢 Tony 取得了进展,但内部选择查询返回多行,在这种情况下为 6 行,但这将取决于放入assessment_category_id 的值。临时表每行更新一次,在这种情况下再次更新 6 行,但更新使用每行中的第一行结果。非常感谢任何帮助。
    • @Mike - 听起来您需要修改相关子查询 (dev.mysql.com/doc/refman/5.5/en/correlated-subqueries.html) 以将内部行链接到外部行。 appraisal_lines 的主键是什么,它与 appraisal_lines_submitted 有什么关系?您已经使用increments.update_perceived_state = appraisal_lines.perceived_state 链接它们,但是哪个字段可以确保只有一个内行更新外行?
    • 谢谢,我将其更改为 SET evaluation_lines_submitted.first_perceived_state = update_perceived_state WHERE sub_categories_id = increments.assessment_sub_category_id 现在可以使用了。感谢您的帮助,非常感谢。
    • 最后声明如下
    • UPDATE appraisal_lines_submitted JOIN(SELECT perceived_state如update_perceived_state,assessment_sub_category_id FROM appraisal_lines WHERE hyperlink_token = 'd7cc7e1adc116e0dac31cbad34cd9a2b322c3507' AND assessment_category_id = '1')AS增量ON increments.update_perceived_state SET appraisal_lines_submitted.first_perceived_state = update_perceived_state WHERE sub_categories_id =增量。评估子类别ID;
    猜你喜欢
    • 2021-05-12
    • 2013-02-07
    • 1970-01-01
    • 2018-06-20
    • 1970-01-01
    • 2012-12-15
    • 2015-12-25
    • 2023-01-18
    • 2013-03-26
    相关资源
    最近更新 更多