Mysql insert update 시 조건 중 primary key 없이 하기
table의 id가 auto_increment인 상태에서 aa1,bb1이 있으면 cnt를 증가시키고 aa1,bb1이 없으면 insert 해보자. 아래 쿼리를 여러번 실행시키면 aa1,bb1의 cnt가 계속 증가한다.(처음에만 insert, 이후에는 update) INSERT INTO {table} SELECT * FROM ( SELECT id, col1, col2, cnt FROM {table} WHERE col1 = 'aa1' AND col2 = 'bb1' UNION SELECT null, 'aa1', 'bb1', 1 ) a ORDER BY id DESC LIMIT 1 ON DUPLICATE KEY UPDATE cnt = cnt + 1;
2022. 12. 1.