当通过表B链接A和C时,如何使用表C中的数据更新表A

时间:2019-04-03 16:16:51

标签: sql oracle oracle-sqldeveloper

我有三个表(ABC),并想用A.appointment_id中的值更新C.tc_appointment_id中的空值。表AC可以通过表B联接。蓝色箭头代表联接,红色箭头代表我要实现的更新。

Visual

我能够将三个表连接在一起,并尝试将我的select语句修改为更新。我成功的选择语句和更新失败。

--Working select

select A.tc_ASN_id,A.appointment_id, B.appt_obj_id, B.appointment_id, C.appointment_id, C.tc_appointment_id from B
join A on B.appt_obj_id = A.asn_id
join C on C.appointment_id = B.appointment_id
where C.appt_status < '9' and A.appointment_id is null;

--Update attempt that ends with SQL Error: ORA-00933: SQL command not properly ended

update asn set appoinmtent_id = ilm_appointments.tc_appointment_id
join ilm_appointment_objects on ilm_appointment_objects.appt_obj_id = asn.asn_id
join ilm_appointments on ilm_appointments.appointment_id = ilm_appointment_objects.appointment_id
where ilm_appointments.appt_status < '9' and asn.appointment_id is null;

预期结果是用null中的值更新A.appointment_id的所有C.tc_appointment_id值。

1 个答案:

答案 0 :(得分:1)

Oracle不支持

UPDATE...JOIN语法。您可以改用相关子查询。

考虑:

UPDATE A
SET A.appointment_id = (
    SELECT C.tc_appointment_id
    FROM B
    INNER JOIN C ON C.appointment_id = B.appointment_id
    WHERE B.appt_obj_id = A.asn_id
)
WHERE A.appointment_id IS NULL;

请注意,子查询必须返回唯一的记录,否则会出现类似“子查询返回多行”的错误。鉴于您的示例数据,这似乎没问题。