简体   繁体   中英

compare 2 columns from 2 different tables and update a column

I want to compare a column in table a and table b

If the a value from table a can be found in table b then I want to update another column in table a with a 'yes' and if it cannot be found I want to say 'no'. This is what I have so far:

UPDATE a 
set
[CCA Match Org] = CASE WHEN b.[serial] = a.[CSI] THEN 'yes' ELSE 'no' END

My error at the moment says:

The column prefix 'b' does not match with a table name or alias name used in the query.

Assuming the join is on b.[serial] = a.[CSI] :

UPDATE a
SET [CCA Match Org] = CASE WHEN b.[serial] IS NOT NULL 
  THEN 'yes' ELSE 'no' END
FROM a LEFT OUTER JOIN b
ON b.[serial] = a.[CSI];

这是一个简单的示例,不确定您是否可以使用,因为您没有向我们提供有关表结构的更多信息。

UPDATE a SET col='Yes' WHERE a.id IN (SELECT a.id FROM a JOIN b ON a.CSI = b.serial)

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