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

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

请建议,我做错了什么。

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

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

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

j2qf4p5b1#

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

  1. UPDATE unit_characteristic uc
  2. SET selected_value_id = cv.id
  3. , fixed_value = NULL
  4. FROM characteristic_value cv
  5. WHERE uc.fixed_value like cv.value || '%'
  6. AND cv.characteristic_id = 6
  7. AND uc.characteristic_id = 6
  8. AND uc.unit_id in (6313, 6314)
  9. ;
p5cysglq

p5cysglq2#

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

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

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

相关问题