数据库
首页 > 数据库> > MySQL SELECT DISTINCT应该区分大小写?

MySQL SELECT DISTINCT应该区分大小写?

作者:互联网

如何使MySQL的SELECT DISTINCT区分大小写?

create temporary table X (name varchar(50) NULL);
insert into X values ('this'), ('This');

现在这个查询:

select distinct(name) from X;

结果是:

this

这里发生了什么?我想SELECT DISTINCT区分大小写.那不应该是默认的吗?

解决方法:

使用BINARY operator

SELECT DISTINCT(BINARY name) AS Name FROM X;

你也可以选择CAST吧:

SELECT DISTINCT 
(CAST(name AS CHAR CHARACTER SET utf8) COLLATE utf8_bin) AS Name FROM X;

SQLFiddle

标签:sql,mysql,case-sensitive,distinct
来源: https://codeday.me/bug/20190930/1836502.html