使用4表连接更新单个表。使用1,2和4中的信息更新表2中的单个列

时间:2014-04-14 15:01:42

标签: sql oracle oracle-sqldeveloper

Oracle数据库

Select语句如下:

SELECT A.DTYP_ID, B.KEM_CLSF, C.KEM_CAT, C.KEM_CLSF, D.DOC_CLSF_ID, D.Dtyp_ID
FROM  AB_CR A, AB_CR_LN B, AB_BD_LN C, AB_BD D
WHERE A.UIDY = B.PARN_OF_LINE_ID
AND B.REFD_LINE_ID = C.UIDY
AND C.PARN_OF_LINE_ID = D.UIDY
AND (a.dtyp_id like '%LR%' or a.dtyp_id like '%CO%')
and B.KEM_CLSF != 'D'
AND D.DTYP_ID LIKE '%AR%'
AND D.DOC_CLSF_ID LIKE '%LNRP%';

需要执行以下操作:

update B.KEM_CLSF to 'D'
where (a.dtyp_id like '%LR%' or a.dtyp_id like '%CO%')
    and B.KEM_CLSF != 'D'
    AND D.DTYP_ID LIKE '%AR%'
    AND D.DOC_CLSF_ID LIKE '%LNRP%';

我无法弄清楚如何正确加入表格!

1 个答案:

答案 0 :(得分:0)

您可以使用相关子查询从AB_CR_LN表中获取满足条件的行 - 如下所示(我没有测试它,代码可能包含错误):

UPDATE AB_CR_LN B
SET B.KEM_CLSF = 'D'    
WHERE B.KEM_CLSF != 'D'
AND EXISTS (
SELECT *
FROM  AB_CR A, AB_BD_LN C, AB_BD D
WHERE A.UIDY = B.PARN_OF_LINE_ID
AND B.REFD_LINE_ID = C.UIDY
AND C.PARN_OF_LINE_ID = D.UIDY
AND (a.dtyp_id like '%LR%' or a.dtyp_id like '%CO%')
AND D.DTYP_ID LIKE '%AR%'
AND D.DOC_CLSF_ID LIKE '%LNRP%'
)