我想执行一个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中可以做到吗?

最佳答案

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)

我看不出左键加入省表的意义-我认为您的更新可以写成:
   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))

关于mysql - 如何更新给定选择条件返回真值的位置?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4140951/

10-10 00:45