Nếu bạn muốn phân biệt không phân biệt chữ hoa chữ thường, bạn cần sử dụng UPPER () hoặc LOWER ().
Trường hợp 1: Sử dụng UPPER ().
Cú pháp như sau:
SELECT DISTINCT UPPER(yourColumnName) FROM yourTableName;
Trường hợp 2: Sử dụng LOWER ().
Cú pháp như sau:
SELECT DISTINCT LOWER(yourColumnName) FROM yourTableName;
Để hiểu cú pháp trên, chúng ta hãy tạo một bảng. Truy vấn để tạo bảng như sau:
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)
Bây giờ bạn có thể chèn một số bản ghi trong bảng bằng cách sử dụng lệnh insert. Truy vấn như sau:
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)
Hiển thị tất cả các bản ghi từ bảng bằng cách sử dụng câu lệnh select. Truy vấn như sau:
mysql> select *from CaseInsensitiveDistinctDemo;
Sau đây là kết quả:
+----+-----------------+--------------+ | 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)
Đây là truy vấn để chọn phân biệt không phân biệt chữ hoa chữ thường.
Trường hợp 1: Sử dụng UPPER (). Truy vấn như sau:
mysql> select distinct upper(UserEmailId) from CaseInsensitiveDistinctDemo;
Sau đây là kết quả:
+--------------------+ | upper(UserEmailId) | +--------------------+ | JOHN@GMAIL.COM | | MIKE@GMAIL.COM | | CAROL@GMAIL.COM | | LARRY@GMAIL.COM | +--------------------+ 4 rows in set (0.06 sec)
Trường hợp 2: Sử dụng LOWER (). Truy vấn như sau:
mysql> select distinct lower(UserEmailId) from CaseInsensitiveDistinctDemo;
Sau đây là kết quả:
+--------------------+ | lower(UserEmailId) | +--------------------+ | john@gmail.com | | mike@gmail.com | | carol@gmail.com | | larry@gmail.com | +--------------------+ 4 rows in set (0.00 sec)