使用INNER Join更新查询

时间:2013-08-07 10:22:33

标签: sql oracle

我有两个表MANUAL_TRANSACTIONS和MANUAL_LIST_TEMP。我想要实现的是使用MANUAL_LIST_TEMP中的信息更新MANUAL_TRANSACTIONS。此处只有MANUAL_LIST_TEMP表中存在的记录应更新为MANUAL_TRANSACTIONS。

我做了类似下面的事情,但是以下语句的问题是它更新了MANUAL_TRANSACTIONS表中的每条记录。

UPDATE MANUAL_TRANSACTIONS 
SET ( "Age", "Assigned_To", "Attachments", "Comments", "Completed_Date_Time"
, "Content_Type", "Created", "Created_By","Cycle_Time (Crt to Complete)"
, "Cycle_Time (First reply)", "DISTRIBUTION_CHANNEL")=
        (SELECT MANUAL_LIST_TEMP."Age", MANUAL_LIST_TEMP."Assigned_To", 
        MANUAL_LIST_TEMP."Attachments", MANUAL_LIST_TEMP."Comments", 
        MANUAL_LIST_TEMP."Completed_Date_Time", MANUAL_LIST_TEMP."Content_Type",
        MANUAL_LIST_TEMP."Created", MANUAL_LIST_TEMP."Created_By", 
        MANUAL_LIST_TEMP."Cycle_Time (Crt to Complete)", 
        MANUAL_LIST_TEMP."Cycle_Time (First reply)", 
        MANUAL_LIST_TEMP."Distribution_Channel" 
        FROM MANUAL_LIST_TEMP 
        WHERE MANUAL_LIST_TEMP.ID = MANUAL_TRANSACTIONS.ID)

5 个答案:

答案 0 :(得分:3)

您还应该将WHERE子句添加到UPDATE语句中:

UPDATE MANUAL_TRANSACTIONS 
SET ( "Age", "Assigned_To", "Attachments", "Comments", "Completed_Date_Time"
, "Content_Type", "Created", "Created_By","Cycle_Time (Crt to Complete)"
, "Cycle_Time (First reply)", "DISTRIBUTION_CHANNEL")=
        (SELECT MANUAL_LIST_TEMP."Age", MANUAL_LIST_TEMP."Assigned_To", 
        MANUAL_LIST_TEMP."Attachments", MANUAL_LIST_TEMP."Comments", 
        MANUAL_LIST_TEMP."Completed_Date_Time", MANUAL_LIST_TEMP."Content_Type",
        MANUAL_LIST_TEMP."Created", MANUAL_LIST_TEMP."Created_By", 
        MANUAL_LIST_TEMP."Cycle_Time (Crt to Complete)", 
        MANUAL_LIST_TEMP."Cycle_Time (First reply)", 
        MANUAL_LIST_TEMP."Distribution_Channel" 
        FROM MANUAL_LIST_TEMP 
        WHERE MANUAL_LIST_TEMP.ID = MANUAL_TRANSACTIONS.ID)
WHERE MANUAL_TRANSACTIONS.ID IN (SELECT MANUAL_LIST_TEMP.ID FROM MANUAL_LIST_TEMP);

为获得最佳效果,请将UPDATE转换为MERGE语句:

MERGE INTO MANUAL_TRANSACTIONS tgt
USING (
        SELECT MANUAL_LIST_TEMP.ID,
        MANUAL_LIST_TEMP."Age", MANUAL_LIST_TEMP."Assigned_To", 
        MANUAL_LIST_TEMP."Attachments", MANUAL_LIST_TEMP."Comments", 
        MANUAL_LIST_TEMP."Completed_Date_Time", MANUAL_LIST_TEMP."Content_Type",
        MANUAL_LIST_TEMP."Created", MANUAL_LIST_TEMP."Created_By", 
        MANUAL_LIST_TEMP."Cycle_Time (Crt to Complete)", 
        MANUAL_LIST_TEMP."Cycle_Time (First reply)", 
        MANUAL_LIST_TEMP."Distribution_Channel" 
        FROM MANUAL_LIST_TEMP
) src
ON (tgt.ID = src.ID)
WHEN MATCHED THEN UPDATE
SET tgt."Age" = src."Age"
  , tgt."Assigned_To" = src."Assigned_To"
  [...]

MERGE语句只会更新MANUAL_TRANSACTIONS目标表)中MANUAL_LIST_TEMP source 表)中具有匹配行的行

答案 1 :(得分:1)

您需要为UPDATE添加一个where子句

答案 2 :(得分:0)

试试这个

 UPDATE MANUAL_TRANSACTIONS 
    Set Age = MANUAL_LIST_TEMP.AGE, ....
    From MANUAL_TRANSACTIONS 
    inner join MANUAL_LIST_TEMP on MANUAL_LIST_TEMP.ID = MANUAL_TRANSACTIONS.ID

答案 3 :(得分:0)

UPDATE MANUAL_TRANSACTIONS mt 
  left join MANUAL_LIST_TEMP mlt 
  on mt.ID = mlt.ID set mt.Age=mlt.Age .....

答案 4 :(得分:0)

请尝试这个:

UPDATE MANUAL_TRANSACTIONS mt INNER JOIN MANUAL_LIST_TEMP mlt 
on mt.ID = mlt.ID set mt.Age=mlt.Age,mt.Assigned_To=mlt.Assigned_To, .....;