简体   繁体   中英

SQL update on one table checking condition from another table

I am querying a column using the below statement:

SELECT t1.col1 from table1 t1, table2 t2  
WHERE t1.col1 = t2.col2 and t2.col3 IN (data1, data2);

I am trying to update the t1.col1 satisfying the above where condition but I get errors.

here is the update statement I tried:

update t1 set t1.col1 = 1 from table1 t1 INNER JOIN table2 t2 where t2.col3 IN ( data1, data2 );

I want to update the value of t1.col1 if the listed data matches the data in t2.col3

You should be able to use correlated subquery syntax here:

UPDATE table1 t1
SET col1 = 1
WHERE EXISTS (SELECT 1 FROM table2 t2 WHERE t2.col2 = t1.col1 AND t2.col3 IN (data1, data2));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM