使用子查询和连接表更新临时表失败

发布于 2024-11-09 15:57:58 字数 2616 浏览 0 评论 0原文

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

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

我的临时表创建如下:

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 使用具有多个返回行的子查询更新字段值

I have an appraisal_lines table in my database and want to take some data from the first appraisal and display it alongside the same data from the latest appraisal so progression from the first to the current appraisal can easily be seen. I am doing this with three values from both appraisals plus other data from the current appraisal.

To do this I think I need to create temporary table to hold both sets of fields plus the additional fields from the current appraisal.

My temporary table is created as follows:

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)
);

I then run an insert and select statement as follows to put the data from the current appraisal into the temporary table:

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;

This works OK.

However when I run the update statement below it fails

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;

with the error

Error Code: 1054
Unknown column 'appraisal_lines.perceived_state' in 'on clause'

I wrote the update statement based on the post : MySQL Update a field value with subquery with multiple returning rows

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不乱于心 2024-11-16 15:57:58

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

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_subscribed 的表加入到名为 increments 的(派生)表中。连接条件引用了 increments 中的列和...等一下,appraisal_lines?那是什么?参与连接的表中没有同名的表。

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

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

Here's your script with some bits highlighted in bold:

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;

Now, in your script you are joining a table named appraisal_lines_submitted to a (derived) table named increments. The joining condition references a column from increments and a column from... wait a minute, appraisal_lines? What is that? There's no table by that name among those taking part in the join.

My guess is, the appraisal_lines.perceived_state reference in the joining condition should be replaced by appraisal_lines_submitted.current_perceived_state.

My other guess is, you missed LIMIT 1 after ORDER BY, which is based on the name of the column being updated (first_perceived_state)

樱&纷飞 2024-11-16 15:57:58

解析器无法看到表 appraisal_lines,因为您正在更新 appraisal_lines_subscribed

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

您还可以删除 ORDER BY code> 在内部 SELECT 中,无论内部数据的顺序如何,都会执行连接。

The parser cannot see the table appraisal_lines because you are updating appraisal_lines_submitted.

The only place you specify the appraisal_lines in within the inner SELECT which is aliased as increments

You can also drop the ORDER BY in the inner SELECT, the join will be performed irrespective of the ordering of the inner data.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文