如果要区分大小写,请使用UPPER()或LOWER()。
情况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)
-> );
现在,您可以使用insert命令在表中插入一些记录。查询如下:mysql> insert into CaseInsensitiveDistinctDemo(UserEmailId,UserPassword) values('John@gmail.com','john123');
mysql> insert into CaseInsensitiveDistinctDemo(UserEmailId,UserPassword) values('john@gmail.com','654321');
mysql> insert into CaseInsensitiveDistinctDemo(UserEmailId,UserPassword) values('Mike@gmail.com','999999');
mysql> insert into CaseInsensitiveDistinctDemo(UserEmailId,UserPassword) values('mike@gmail.com','334556');
mysql> insert into CaseInsensitiveDistinctDemo(UserEmailId,UserPassword) values('Carol@gmail.com','1010101');
mysql> insert into CaseInsensitiveDistinctDemo(UserEmailId,UserPassword) values('Larry@gmail.com','12345678');
使用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)
这是选择不区分大小写的区分查询。
情况1:使用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)
情况2:使用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)