728x90
반응형

INSERT문에 SELECT문 사용하기

✔ 문법

INSERT INTO tbl_temp2 (fld_id)
  SELECT tbl_temp1.fld_order_id
  FROM tbl_temp1 WHERE tbl_temp1.fld_order_id > 100;

 


✔ 방법

1️⃣ 모든 컬럼 복사

INSERT INTO 목적지테이블
SELECT * FROM 출발지테이블

 

2️⃣ 지정 컬럼 복사

INSERT INTO 목적지테이블
(컬럼명1, 컬럼명2, 컬럼명3)
SELECT 컬럼명1, 컬럼명2, 컬럼명3
FROM 출발지테이블

 


✔ 예시 👇

INSERT INTO username_history (user_id, user_name) SELECT user_id, user_name FROM user
INSERT INTO username_history (user_id, user_name) SELECT user_id, user_name FROM user ORDER BY user_id

참고 : https://zetawiki.com/wiki/SQL_SELECT_%EA%B2%B0%EA%B3%BC%EB%A5%BC_INSERT_%ED%95%98%EA%B8%B0

반응형
복사했습니다!