使用join更新表

时间:2011-05-11 22:10:58

标签: sql oracle

我试图通过在查询中传递值来更新表。请注意,该值不是来自另一个表。但是,我用来识别整个板块中常见项目的关键是在另一个表中。我如何加入这两个以确保我更新正确的记录? 数据库:Oracle 10g 在Toad中运行查询 请帮忙。

这是我到目前为止所做的:

UPDATE HCS.ITEMINVENTORY
SET HCS.ITEMINVENTORY.BATCHGROUPING = 'ADMIXED'
WHERE (ITEMINVENTORY.FACILITYCODE = 'B')
    AND (ITEMINVENTORY.FORMULARYSTATUS = 'Y')
    AND (ITEMDISPENSESIZE.ITEMID = ITEMINVENTORY.ITEMID)"Notice the second table"
    AND (HCS.ITEMNUMBER IN (18501346,18800284)

2 个答案:

答案 0 :(得分:1)

如果您只想应用ITEMDISPENSESIZE中存在的更新,请使用EXISTS子查询。像这样:

UPDATE HCS.ITEMINVENTORY
SET HCS.ITEMINVENTORY.BATCHGROUPING = 'ADMIXED'
WHERE (ITEMINVENTORY.FACILITYCODE = 'B')
    AND (ITEMINVENTORY.FORMULARYSTATUS = 'Y')
    AND EXISTS (
        SELECT 1
        FROM   ITEMDISPENSESIZE
        WHERE  ITEMDISPENSESIZE.ITEMID = ITEMINVENTORY.ITEMID
        AND    ITEMDISPENSESIZE.ITEMNUMBER IN ('18501346','18800284')
    );

答案 1 :(得分:-1)

试试这个:

update Table
   set column1 = 'new value'
   from table1 a, table2 b
   where a.column2 = x 
   and a.column3 = y
   and b.column4 = z