본문 바로가기
MySql

MySQL 트리거에 최근 삽입 된 ID를 가져 오시겠습니까?

by 베이스 공부 2020. 12. 8.
반응형

트리거를 사용하여 행을 삽입하고 후속 쿼리에서 사용하기 위해 마지막으로 생성 된 ID를 사용하고 싶습니다.

어떻게 할 수 있습니까?

코드는 다음과 같습니다.

BEGIN
IF (NEW.counter >= 100) THEN
INSERT INTO tagCategories (name, counter) VALUES ('unnamed', NEW.counter);
// here i want to have access to the above inserted id
UPDATE tagCategories2tagPairs SET tagCategoryId = <<ID_HERE>> WHERE tagPairId = OLD.id
END IF;
END

 

해결 방법

 


A를 사용하여 여러 행을 삽입하는 경우 single INSERT statement, LAST_INSERT_ID() returns the value generated for the first inserted row 뿐.

 

참조 페이지 https://stackoverflow.com/questions/2263453

 

 

반응형

댓글