UPDATE INNER JOIN on子句中的未知列

时间:2019-01-21 19:14:04

标签: mysql phpmyadmin

我知道这个问题已经有了一些答案,但似乎没有一个对我有用。

基本上,我所完成的任务不过是随着时间推移而预设的任务:我想在截止日期之前更新任务日期;同样,在截止日期之前,我需要根据预设值更新组件表的值。

我成功进行了查询以更新截止日期,如下所示:

UPDATE tasks

    SET tasks.start_date = DATE_ADD(
    tasks.start_date, INTERVAL tasks.frequency SECOND
    )

    WHERE tasks.start_date <= CURRENT_DATE AND tasks.hour <= CURRENT_TIME

当我尝试在截止日期的同时更新组件行时,我无法使其正常工作。我的查询看起来像这样:

    UPDATE tasks,components 

    INNER JOIN presets ON presets.id = tasks.id_preset
    INNER JOIN preset_values ON preset_values.id_preset = presets.id
    INNER JOIN preset_values AS pv2 ON pv2.serial_number = 
    components.serial_number 

    SET tasks.start_date = DATE_ADD(
    tasks.start_date, INTERVAL tasks.frequency SECOND
    ),
    components.value = pv2.value,
    components.state = pv2.on_off

    WHERE tasks.start_date <= CURRENT_DATE AND tasks.hour <= CURRENT_TIME

我收到的消息错误是: MySQL说:文档

1054-“子句”中的未知列“ tasks.id_preset”

您能帮我使此查询生效吗?谢谢

components表:https://i.stack.imgur.com/VOZX8.png

预设表:https://i.stack.imgur.com/Fs7yO.png

preset_values表:https://i.stack.imgur.com/AZkcm.png

任务表:https://i.stack.imgur.com/0qSEe.png

EDIT :三思后,我选择了两个单独的更新查询。目前看起来像这样。如果有人有一个更新解决方案,那么如果性能更好,我将很高兴使用它。否则,我暂时将坚持这种解决方案。谢谢大家的帮助。

BEGIN
    UPDATE
        components AS c1
    INNER JOIN preset_values AS pv1
    ON
        pv1.serial_number = c1.serial_number
    INNER JOIN tasks AS t1
    ON
        t1.id_preset = pv1.id_preset
    SET
        c1.value = pv1.value,
        c1.state = pv1.on_off
    WHERE
        t1.start_date <= CURRENT_DATE AND t1.hour <= CURRENT_TIME AND t1.on_off=1;
    UPDATE
        tasks
    SET
        tasks.start_date = DATE_ADD(
            tasks.start_date,
            INTERVAL tasks.frequency SECOND
        )
    WHERE
        tasks.start_date <= CURRENT_DATE AND tasks.hour <= CURRENT_TIME AND t1.on_off=1 ;
END

2 个答案:

答案 0 :(得分:0)

您需要在where子句上连接taskscomponents。现在,他们是交叉联结的。

答案 1 :(得分:0)

如果出于某种原因(确实不太可能)确实确实需要交叉联接,则应使用CROSS JOIN子句或将逗号分隔的表交换到components, tasks中。您的查询

UPDATE tasks,components INNER JOIN presets ON presets.id = tasks.id_preset ...

表示

UPDATE tasks, (components INNER JOIN presets ON presets.id = tasks.id_preset) ...

很显然tasks.id_preset中没有components INNER JOIN presets ON presets.id = tasks.id_preset

但是,交叉联接意味着将每个过滤的显式联接行与表components的每一行合并。每行的最后一行将执行

SET
components.value = pv2.value,
components.state = pv2.on_off

我不认为这是故意的。如果对笛卡尔积的这种操作确实是您想要的,请三思。

serial_number中有一个components,所以您很可能想要

UPDATE     tasks
INNER JOIN presets       ON presets.id               = tasks.id_preset
INNER JOIN preset_values ON preset_values.id_preset  = presets.id
INNER_JOIN components    ON components.serial_number = preset_values.serial_number 

SET ...