通过连接3个表来进行UPDATE-SET查询

时间:2012-08-16 07:09:54

标签: sql oracle join sql-update

我有3张桌子,比如RELATIONS,PERSON,COMPANY 我需要加入这3个表格UPDATE active_flag PERSON'I'/'A'字段。 我的要求就像

UPDATE PERSON p
SET p.active_flag ='I'
FROM PERSON p,RELATIONS r,COMPANY c
WHERE p.email ='email@gmail.com'
AND c.rel_id = r.id
AND r.dep_id = '1234567'
AND r.book_id = '1234567'

我该怎么做?

2 个答案:

答案 0 :(得分:1)

- 不确定加入

UPDATE p
SET 
    p.[active_flag] ='I'
FROM Person p
    INNER JOIN Relations r ON
        r.[vNumber] = p.[vesionNo]
    INNER JOIN Company c ON
        c.[rel_id] = r.[id]
WHERE
    p.[email] ='email@gmail.com'
AND r.[dep_id] = '1234567' 
AND r.[book_id] = '1234567'

答案 1 :(得分:1)

merge into person p
using (
       select id
       FROM PERSON p,RELATIONS r,COMPANY c
       WHERE p.email ='email@gmail.com'
             AND c.rel_id = r.id
             AND r.dep_id = '1234567'
             AND r.book_id = '1234567'
      ) pu
on (pu.id = p.id)
when matched then update set p.active_flag ='I'