MySQL SELECT DISTINCT should be case sensitive?

前端 未结 3 502
旧巷少年郎
旧巷少年郎 2021-02-05 14:49

How do I make MySQL\'s SELECT DISTINCT case sensitive?

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


        
3条回答
  •  执念已碎
    2021-02-05 15:17

    Use BINARY operator for that:

    SELECT DISTINCT(BINARY name) AS Name FROM X;
    

    You can also CAST it while selecting:

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

    See this SQLFiddle

提交回复
热议问题