How can I make SQL case sensitive string comparison on MySQL?

前端 未结 11 2196
温柔的废话
温柔的废话 2020-11-22 02:45

I have a function that returns five characters with mixed case. If I do a query on this string it will return the value regardless of case.

How can I make MySQL stri

11条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-22 03:03

    You can use BINARY to case sensitive like this

    select * from tb_app where BINARY android_package='com.Mtime';
    

    unfortunately this sql can't use index, you will suffer a performance hit on queries reliant on that index

    mysql> explain select * from tb_app where BINARY android_package='com.Mtime';
    +----+-------------+--------+------------+------+---------------+------+---------+------+---------+----------+-------------+
    | id | select_type | table  | partitions | type | possible_keys | key  | key_len | ref  | rows    | filtered | Extra       |
    +----+-------------+--------+------------+------+---------------+------+---------+------+---------+----------+-------------+
    |  1 | SIMPLE      | tb_app | NULL       | ALL  | NULL          | NULL | NULL    | NULL | 1590351 |   100.00 | Using where |
    +----+-------------+--------+------------+------+---------------+------+---------+------+---------+----------+-------------+
    

    Fortunately, I have a few tricks to solve this problem

    mysql> explain select * from tb_app where android_package='com.Mtime' and BINARY android_package='com.Mtime';
    +----+-------------+--------+------------+------+---------------------------+---------------------------+---------+-------+------+----------+-----------------------+
    | id | select_type | table  | partitions | type | possible_keys             | key                       | key_len | ref   | rows | filtered | Extra                 |
    +----+-------------+--------+------------+------+---------------------------+---------------------------+---------+-------+------+----------+-----------------------+
    |  1 | SIMPLE      | tb_app | NULL       | ref  | idx_android_pkg           | idx_android_pkg           | 771     | const |    1 |   100.00 | Using index condition |
    +----+-------------+--------+------------+------+---------------------------+---------------------------+---------+-------+------+----------+-----------------------+  
    

提交回复
热议问题