개요 : MySQL은 NOT NULL 컬럼에 '' 과 ' ' 입력을 허용하여서 정제되지 않은 데이터의 경우나 개발자들이 '' 을 입력하고 조회에 조건으로 사용하는 경우가 있으므로 아래와 같이 조회되는 방법과 차이점을 알아두자.
'' : 공백 , ' ' : 스페이스 라고 하자.
create table test(
col1 varchar(20),
col2 varchar(20));
insert into test values('', '111');
insert into test values(' ', '112');
insert into test values(' ', '113');
insert into test values(' a a ', '114');
insert inot test values('a a', '115');
insert into test values('222', '116');
select * from test; -- 임의로 '(싱글쿼테이션) 사용해서 결과 표시
col1 | col2 |
------------------
'' | 111 |
' ' | 112 |
' ' | 113 |
' a a ' | 114 |
'a a' | 115 |
'222' | 116 |
------------------
select ascii(''), ascii(' '); -- 공백: 0, 스페이스 : 32
select * from test where col1 = '';
col1 | col2 |
------------------
'' | 111 |
select * from test where ascii(col1) = 0;
col1 | col2 |
------------------
'' | 111 |
select a.*, ascii(col1), ascii(trim(col1)), ascci('a'), instr(col1,' ') from test a where ascii(col1) = 32; -- 첫 문자 스페이스 있는 데이터 모두 출력
col1 | col2 | ascii(col1) | ascii(trim(col1)) | ascii('a') | instr(col1,' ') |
-----------------------------------------------------------------------------------
' ' | 112 | 32 | 0 | 97 | 1 |
' ' | 113 | 32 | 0 | 97 | 1 |
' a a ' | 114 | 32 | 97 | 97 | 1 |
select * from test where col1 = ' ';
col1 | col2 |
------------------
' ' | 112 |
select * from test where trim(col1) = '';
col1 | col2 |
------------------
'' | 111 |
' ' | 112 |
' ' | 113 |'MySQL > MySQL' 카테고리의 다른 글
| MySQL slow_query 느린 쿼리 추출 (0) | 2025.04.02 |
|---|---|
| MySQL Parallel 제한사항(Cluster storage configuration 확인) (0) | 2025.03.26 |
| NULL 정렬 (0) | 2025.01.22 |
| 인덱스 엑세스 조건 등치+Between 무시 -> 등치+IN (Subquery) 로 변경 (0) | 2025.01.16 |
| Hash Join (0) | 2024.12.26 |