我想知道如何重新考虑这个脚本因为两个嵌套查询非常相似(我正在使用ORACLE作为我的数据库):
UPDATE
ALERT alert
SET
VERIFICATION_COUNT =
(
SELECT COUNT(*)
FROM TRANSCRIPTION trans
INNER JOIN DOS dos
ON trans.REF_NO = dos.REF_NO
WHERE trans.STATUS = 'ONGOING'
AND alert.VISIT = dos.VISIT
)
WHERE
alert.VISIT =
(
SELECT DISTINCT dos.VISIT FROM TRANSCRIPTION trans
INNER JOIN DOS dos
ON trans.REF_NO = dos.REF_NO
WHERE trans.STATUS = 'ONGOING'
AND alert.VISIT = dos.VISIT
);
答案 0 :(得分:0)
试试这个:
merge into alert A
using (
select D.visit, count(*) as visit_count
from transcription T
join dos D on D.ref_no = T.ref_no
where T.status = 'ONGOING'
group by D.visit
) X
on ( A.visit = X.visit )
when matched then
update
set A.verification_count = X.visit_count
;
但是,我并不保证它与原作完全相同。 :-)那是因为在不知道外键关系的情况下,我对原始WHERE子句的作用有点困惑,因为对结果集使用了相等运算符,而不是对标量子查询。