Developer Factory

update, insert문을 select로 가져와서 활용하기 본문

Developer/DBMS

update, insert문을 select로 가져와서 활용하기

Jeremy.Park 2014. 7. 3. 10:33

- select를 활용한 update 하기
table1 업데이트할 테이블명
table2 업데이트할 데이터를 가져올 테이블명

update table1 a
  set a.column1 = 
    ( select b.column1 from table2 b
          where b.column2 = 'xxx' )
  where a.column3 = 'xxx'; 


- 같은 테이블에서 컬럼 하나만 변경하여 내용 복사해서 넣기
x=col2의 변경코자 하는 값

insert into table a (col1, col2, col3, col4 ..)
    select b.col1, x, b.col3, b.col4 ..
        from table b where col1='조건'; 


참고 블로그 : http://sreco.tistory.com/25






여러 행 SELECT해서 INSERT 하기

INSERT INTO table_a
( title, name, regdate, register, memberid, categoryid )
SELECT title, name, regdate, register, memberid, categoryid
FROM table_b
WHERE categoryid=10