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('[email protected]','john123'); Query OK, 1 row affected (0.15 sec) mysql> insert into CaseInsensitiveDistinctDemo(UserEmailId,UserPassword) values('[email protected]','654321'); Query OK, 1 row affected (0.43 sec) mysql> insert into CaseInsensitiveDistinctDemo(UserEmailId,UserPassword) values('[email protected]','999999'); Query OK, 1 row affected (0.14 sec) mysql> insert into CaseInsensitiveDistinctDemo(UserEmailId,UserPassword) values('[email protected]','334556'); Query OK, 1 row affected (0.16 sec) mysql> insert into CaseInsensitiveDistinctDemo(UserEmailId,UserPassword) values('[email protected]','1010101'); Query OK, 1 row affected (0.13 sec) mysql> insert into CaseInsensitiveDistinctDemo(UserEmailId,UserPassword) values('[email protected]','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 | [email protected] | john123 | | 2 | [email protected] | 654321 | | 3 | [email protected] | 999999 | | 4 | [email protected] | 334556 | | 5 | [email protected] | 1010101 | | 6 | [email protected] | 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) | +--------------------+ | [email protected] | | [email protected] | | [email protected] | | [email protected] | +--------------------+ 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) | +--------------------+ | [email protected] | | [email protected] | | [email protected] | | [email protected] | +--------------------+ 4 rows in set (0.00 sec)