MySQL에서 대소문자를 구분하지 않는 DISTINCT 사용 방법
MySQL에서 대소문자 차이를 무시하고 중복 데이터를 제거하려면 DISTINCT와 함께 UPPER() 또는 LOWER() 함수를 사용해야 합니다. 이메일 주소처럼 'John@gmail.com'과 'john@gmail.com'이 사실상 같은 값으로 취급되어야 하는 경우에 특히 유용합니다.
방법 1: UPPER() 함수 사용
모든 값을 대문자로 변환한 뒤 중복을 제거하는 방식입니다. 기본 문법은 다음과 같습니다.
SELECT DISTINCT UPPER(yourColumnName) FROM yourTableName;
방법 2: LOWER() 함수 사용
반대로 모든 값을 소문자로 변환한 뒤 중복을 제거하는 방식입니다. 기본 문법은 다음과 같습니다.
SELECT DISTINCT LOWER(yourColumnName) FROM yourTableName;
예제 테이블 생성
위 문법을 실제로 확인하기 위해 테이블을 하나 만들어 보겠습니다. 테이블 생성 쿼리는 다음과 같습니다.
mysql> create table CaseInsensitiveDistinctDemo -> ( -> Id int NOT NULL AUTO_INCREMENT, -> UserEmailId varchar(30), -> UserPassword varchar(10), -> PRIMARY KEY(Id) -> ); Query OK, 0 rows affected (0.64 sec)
이제 INSERT 명령으로 몇 개의 레코드를 삽입합니다. 같은 이메일 주소가 대소문자만 다르게 저장되어 있는 점에 주목하세요.
mysql> insert into CaseInsensitiveDistinctDemo(UserEmailId,UserPassword) values('John@gmail.com','john123');
Query OK, 1 row affected (0.15 sec)
mysql> insert into CaseInsensitiveDistinctDemo(UserEmailId,UserPassword) values('john@gmail.com','654321');
Query OK, 1 row affected (0.43 sec)
mysql> insert into CaseInsensitiveDistinctDemo(UserEmailId,UserPassword) values('Mike@gmail.com','999999');
Query OK, 1 row affected (0.14 sec)
mysql> insert into CaseInsensitiveDistinctDemo(UserEmailId,UserPassword) values('mike@gmail.com','334556');
Query OK, 1 row affected (0.16 sec)
mysql> insert into CaseInsensitiveDistinctDemo(UserEmailId,UserPassword) values('Carol@gmail.com','1010101');
Query OK, 1 row affected (0.13 sec)
mysql> insert into CaseInsensitiveDistinctDemo(UserEmailId,UserPassword) values('Larry@gmail.com','12345678');
Query OK, 1 row affected (0.20 sec)SELECT 문으로 테이블의 전체 레코드를 조회해 보겠습니다.
mysql> select *from CaseInsensitiveDistinctDemo;
실행 결과는 다음과 같습니다.
+----+-----------------+--------------+ | Id | UserEmailId | UserPassword | +----+-----------------+--------------+ | 1 | John@gmail.com | john123 | | 2 | john@gmail.com | 654321 | | 3 | Mike@gmail.com | 999999 | | 4 | mike@gmail.com | 334556 | | 5 | Carol@gmail.com | 1010101 | | 6 | Larry@gmail.com | 12345678 | +----+-----------------+--------------+ 6 rows in set (0.00 sec)
테이블에는 총 6개의 레코드가 있지만, 대소문자를 무시하면 실제 고유한 이메일은 4개입니다. 이제 대소문자를 구분하지 않는 DISTINCT 조회 쿼리를 살펴보겠습니다.
UPPER() 적용 결과
UPPER() 함수를 사용한 쿼리는 다음과 같습니다.
mysql> select distinct upper(UserEmailId) from CaseInsensitiveDistinctDemo;
실행 결과는 다음과 같습니다.
+--------------------+ | upper(UserEmailId) | +--------------------+ | JOHN@GMAIL.COM | | MIKE@GMAIL.COM | | CAROL@GMAIL.COM | | LARRY@GMAIL.COM | +--------------------+ 4 rows in set (0.06 sec)
LOWER() 적용 결과
LOWER() 함수를 사용한 쿼리는 다음과 같습니다.
mysql> select distinct lower(UserEmailId) from CaseInsensitiveDistinctDemo;
실행 결과는 다음과 같습니다.
+--------------------+ | lower(UserEmailId) | +--------------------+ | john@gmail.com | | mike@gmail.com | | carol@gmail.com | | larry@gmail.com | +--------------------+ 4 rows in set (0.00 sec)
두 방법 모두 원래 6개였던 레코드가 대소문자를 무시한 기준으로 4개의 고유한 값만 반환되는 것을 확인할 수 있습니다. 참고로 MySQL의 기본 문자셋(collation) 설정에 따라 WHERE 절 비교는 대소문자를 구분하지 않는 경우가 많지만, DISTINCT 자체는 저장된 값 그대로 비교하므로 위와 같이 UPPER()나 LOWER()로 값을 정규화한 후 처리하는 것이 가장 확실한 방법입니다.