How can I update where this selection condition returns the true value?

I want to perform an SQL update

UPDATE  incCustomer SET etaxCode = 'HST' WHERE 

only for entries where this is true.

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 )

I do not think this is possible with ANSI SQL, but is it possible to do this in MySQL?

+3
source share
1 answer

Syntax MySQL UPDATE supports joins in both ANSI-89 and ANSI-92 syntax.

Using:

   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)

I do not see the meaning of LEFT JOINING in the provinces table - I think your UPDATE can be written as:

   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))
+4
source

Source: https://habr.com/ru/post/1774002/


All Articles