使用左外部联接的postgres更新不起作用

7kjnsjlb  于 2021-07-29  发布在  Java
关注(0)|答案(2)|浏览(377)

请建议,我做错了什么。

UPDATE uc 
SET uc.selected_value_id = cv.id, uc.fixed_value = NULL
FROM unit_characteristic uc
left JOIN characteristic_value cv ON uc.fixed_value like CONCAT(cv.value,'%')  
WHERE cv.characteristic_id = 6 
and uc.characteristic_id = 6
and uc.unit_id in (6313,6314)

获取错误
sql错误[42p01]:错误:关系“uc”不存在位置:8 org.postgresql.util.psqlexception:错误:关系“uc”不存在位置:8
而这个选择工作正常

select count(uc.*)   
FROM unit_characteristic uc
left JOIN characteristic_value cv ON uc.fixed_value like CONCAT(cv.value,'%')  
WHERE cv.characteristic_id = 6 
and uc.characteristic_id = 6
and uc.unit_id in (6313,6314)
j2qf4p5b

j2qf4p5b1#

您不必在中重复目标表 FROM 条款;它已经在范围表中
目标表可以有别名
但是,这个 SET columnname = new_value 行不能使用此别名。它是隐式的(因为只有一个表引用要更新)

UPDATE unit_characteristic uc 
   SET selected_value_id = cv.id
     , fixed_value = NULL
FROM characteristic_value cv 
WHERE uc.fixed_value like cv.value || '%'
 AND cv.characteristic_id = 6
 AND uc.characteristic_id = 6
 AND uc.unit_id in (6313, 6314)
   ;
p5cysglq

p5cysglq2#

在postgres中,在 update 无法引用中的表 from . 我怀疑你想要:

update unit_characteristic uc 
    set selected_value_id = cv.id,
        fixed_value = NULL
from characteristic_value cv 
where uc.fixed_value like cv.value || '%' and
      cv.characteristic_id = 6 and
      uc.characteristic_id = 6 and
      uc.unit_id in (6313, 6314);

请注意,您的查询版本使用 left join . 但是 where 子句将其转换为内部连接anway。

相关问题