如何更新给定选择条件返回真值的位置?

时间:2010-11-10 03:21:28

标签: mysql sql sql-update

我想执行SQL更新

UPDATE  incCustomer SET etaxCode = 'HST' WHERE 

仅适用于记录确实如此。

select  etaxCode
from  incCustomer
left join incAddress on incCustomer.iAddressId = incAddress.iId
left join incProvince on incAddress.iProvinceStateId = incProvince.iId
where incAddress.iProvinceStateId in (     2  ,     3   ,      4   ,    6   ,   9 )

我不认为这可以用ANSI SQL实现,但可以在MySQL中完成吗?

1 个答案:

答案 0 :(得分:4)

MySQL UPDATE syntax支持ANSI-89和ANSI-92语法中的连接。

使用:

   UPDATE INCCUSTOMER c
LEFT JOIN INCADDRESS a ON a.iid = c.iaddressid
LEFT JOIN INCPROVINCE p ON p.iid = a.iprovincestateid
      SET c.etaxcode = 'HST'
    WHERE a.iProvinceStateId IN (2,3,4,6,9)

我没有看到LEFT JOIN到省表的重点 - 我认为你的UPDATE可以写成:

   UPDATE INCCUSTOMER 
      SET c.etaxcode = 'HST'
    WHERE EXISTS(SELECT NULL
                   FROM INCADDRESS a
                  WHERE a.iid = INCCUSTOMER.iaddressid
                    AND a.iProvinceStateId IN (2,3,4,6,9))