如何使MySQL的SELECT DISTINCT区分大小写?
create temporary table X (name varchar(50) NULL);
insert into X values ('this'), ('This');
现在这个查询:
select distinct(name) from X;
结果:
此
这里发生了什么?我想SELECT DISTINCT区分大小写。这不应该是默认的吗?
答案 0 :(得分:25)
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;
答案 1 :(得分:7)
我宁愿将列定义更新为区分大小写的碰撞。
像这样:
create table X (name VARCHAR(128) CHARACTER SET utf8 COLLATE utf8_bin NULL);
insert into X values ('this'), ('This');
SQLFiddle:http://sqlfiddle.com/#!2/add276/2/0
答案 2 :(得分:2)