문서의 선택한 두 판 사이의 차이를 보여줍니다.
다음 판 | 이전 판 | ||
database:mysql:functions [2012/08/10 20:15] kwon37xi 새로 만듦 |
database:mysql:functions [2015/11/15 18:36] (현재) kwon37xi [마지막 Insert ID] |
||
---|---|---|---|
줄 1: | 줄 1: | ||
====== MySQL Functions ====== | ====== MySQL Functions ====== | ||
===== 마지막 Insert ID ===== | ===== 마지막 Insert ID ===== | ||
- | * [[http:// | + | * [[https:// |
- | <code sql> | + | |
SELECT LAST_INSERT_ID(); | SELECT LAST_INSERT_ID(); | ||
</ | </ | ||
+ | * 이 함수는 기본적으로 insert 문을 실행하여 '' | ||
+ | * 만약 '' | ||
+ | -- 아래와 같은 형태로 하면 0, null 이외의 PK 값에 대해서도 last_insert_id()를 믿을 수 있게 된다. | ||
+ | INSERT INTO some_table (primary_key) values(last_insert_id(직접지정_PK값)); | ||
+ | SELECT last_insert_id(); | ||
+ | </ | ||
+ | * 단일 insert 문에서 여러 row를 insert하면, | ||
+ | INSERT INTO t VALUES (NULL, ' | ||
+ | SELECT last_insert_id(); | ||
+ | </ | ||
+ | * '' | ||
+ | ===== 조건적 count ===== | ||
+ | count 함수는 null이면 세지 않고 null 이외의 값이면 1씩 더하는 특징이 있다. if 문과 조합하여 조건적 count를 할 수 있다. | ||
+ | <code sql> | ||
+ | select name, count(1) as total, count(if(age < 20, 1, null)) as YOUNG from users | ||
+ | group by name | ||
+ | </ | ||