programing

조건에 따라 다른 테이블의 열로 열 값 업데이트

sourcejob 2023. 1. 27. 21:18
반응형

조건에 따라 다른 테이블의 열로 열 값 업데이트

테이블이 두 개 있는데...

table1(id, item, price) 값:

id | item | price
-------------
10 | book | 20  
20 | copy | 30   
30 | pen  | 10

......표2(id, item, price) 값:

id | item | price
-------------
10 | book | 20
20 | book | 30

다음 작업을 수행합니다.

update table1 
   set table1.Price = table2.price 
 where table1.id = table2.id
   and table1.item = table2.item.

제가 그걸 어떻게 합니까?

다음과 같은 방법으로 해결할 수 있습니다.

UPDATE table1 
   SET table1.Price = table2.price 
   FROM table1  INNER JOIN  table2 ON table1.id = table2.id

다음의 조작도 실행할 수 있습니다.

UPDATE table1 
   SET price=(SELECT price FROM table2 WHERE table1.id=table2.id);

이것은 확실히 효과가 있습니다.

UPDATE table1
SET table1.price=(SELECT table2.price
  FROM table2
  WHERE table2.id=table1.id AND table2.item=table1.item);

언급URL : https://stackoverflow.com/questions/1746125/update-columns-values-with-column-of-another-table-based-on-condition

반응형