복붙노트

[SQL] 조건에 따라 다른 테이블의 열과 업데이트 열 값 [중복]

SQL

조건에 따라 다른 테이블의 열과 업데이트 열 값 [중복]

나는 두 개의 테이블을 가지고 ...

표 (ID, 품목, 가격) 값 :

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

.... 표 2 (ID, 품목, 가격) 값 :

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.

내가 어떻게해야합니까?

해결법

  1. ==============================

    1.이 같은 뭔가를해야한다 :

    이 같은 뭔가를해야한다 :

    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);
    
  2. ==============================

    2.이것은 확실히 작동합니다 :

    이것은 확실히 작동합니다 :

    UPDATE table1
    SET table1.price=(SELECT table2.price
      FROM table2
      WHERE table2.id=table1.id AND table2.item=table1.item);
    
  3. from https://stackoverflow.com/questions/1746125/update-columns-values-with-column-of-another-table-based-on-condition by cc-by-sa and MIT license