我有三个表(A
,B
和C
),并想用A.appointment_id
中的值更新C.tc_appointment_id
中的空值。表A
和C
可以通过表B
联接。蓝色箭头代表联接,红色箭头代表我要实现的更新。
我能够将三个表连接在一起,并尝试将我的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
值。
答案 0 :(得分:1)
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;
请注意,子查询必须返回唯一的记录,否则会出现类似“子查询返回多行”的错误。鉴于您的示例数据,这似乎没问题。