复杂更新声明

时间:2015-11-17 20:28:39

标签: mysql select join

我有一个位置表(its_locations),其中包含以下列:

its_locations.state_tag (FK),   
its_locations.state_tag_temp (empty)

还有另一个表its_equipment,它包含以下列:

Its_equipment.its_equipment_id (PK),  
its_equipment.state_tag (contains the tag number of the equipment)  

我需要将相应的its_equipment.state_tag号码复制到its_locations.state_tag_temp列。

我尝试了以下(以及其他想法)但没有成功并收到错误

  

不唯一的表/别名:“its_equipment”。

我意识到它与引用同一个表两次有关,我尝试了不同的别名,但没有成功。任何帮助将不胜感激。

update its_locations
set its_locations.state_tag_temp = 
    (SELECT its_equipment.state_tag
    FROM its_equipment)

where its_locations.state_tag = 
    (SELECT its_equip_id
    FROM its_equipment
    join its_equipment
    on its_locations.state_tag = its_equipment.its_equip_id);

2 个答案:

答案 0 :(得分:1)

好的,我明白了。我这太复杂了。这是完成我想要的代码。

update its_locations
set state_tag_temp = 
    (select its_equipment.state_tag
    from its_equipment
    where its_locations.state_tag = its_equipment.its_equip_id);

答案 1 :(得分:0)

看起来你的第二个子选择引用了同一个表,但你想引用回到its_locations表。你有FROM_equipment JOIN its_locations,你想要的是FROM its_equipment JOIN its_locations。

update its_locations
set its_locations.state_tag_temp = 
(SELECT its_equipment.state_tag
FROM its_equipment)

where its_locations.state_tag = 
(SELECT its_equip_id
FROM its_equipment its1
join its_locations its2
on its2.state_tag = its_equipment.its_equip_id);