DB
Insert문 실행 후 PK ID 조회하기
herojoon
2023. 1. 26. 01:54
반응형
Insert문 실행 후 PK ID 조회하기
-- Insert Query의 ID 조회
SELECT LAST_INSERT_ID();
예시
-- Insert Query (Auto Increment 사용)
INSERT INTO board (id, title, content, user, create_dt)
VALUES (NULL, 'hello', 'happy christmas!!', 'herojoon', NOW());
-- Insert Query의 ID 조회
SELECT LAST_INSERT_ID();
/*
Insert Query
(Auto Increment 사용 중 해당 ID값을 입력하여 Insert하여도
Insert Query문이 성공하며 아래 LAST_INSERT_ID 조회에서도 값이 잘 나옵니다.)
*/
INSERT INTO board (id, title, content, user, create_dt)
VALUES (1, 'hello', 'happy christmas!!', 'herojoon', NOW());
-- Insert Query의 ID 조회
SELECT LAST_INSERT_ID();
반응형