我的数据库中有一个appraisal_lines表,想要从第一次评估中获取一些数据,并将其与最新评估中的相同数据一起显示,这样可以很容易地看到从第一次评估到当前评估的进展。我使用来自评估的三个值加上当前评估的其他数据来做这个。
要做到这一点,我想我需要创建临时表来保存两组字段以及当前评估中的其他字段。
我的临时表创建如下:
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和select语句,将当前评估中的数据放入临时表中:
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
中的未知列'appraisal_lines.perceived_state'
'on clause'
我根据帖子MySQL Update a field value with subquery with multiple returning rows
撰写了更新声明答案 0 :(得分:2)
这是你的脚本,其中一些位用粗体突出显示:
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
。
我的另一个猜测是,您在LIMIT 1
之后错过了ORDER BY
,这是基于要更新的列的名称(first_perceived_state
)
答案 1 :(得分:0)
解析器无法查看表appraisal_lines
,因为您正在更新appraisal_lines_submitted
。
您在内部appraisal_lines
内指定SELECT
的唯一位置,其别名为increments
您也可以将ORDER BY
放在内部SELECT
中,无论内部数据的排序如何,都会执行连接。