MySQL Search: LIKE for MySQL case-sensitive queries

The LIKE statement is used to search for records with partial strings in MySQL. By default, LIKE statement queries will match case-insensitive records. This means that the query will match records in both lowercase and uppercase letters.
For example, to search for all records whose table name starts with "Gr":
mysql> SELECT name FROM colors WHERE name LIKE 'Gr%';
Insert image description here

 You can see that the above query can match records with any uppercase or lowercase case.
However, sometimes you just need to select case-sensitive data. In this case you need to convert the value to binary.
To do this, add the BINARY option and similar status, and see the results:
mysql> SELECT name FROM colors WHERE name LIKE BINARY 'Gr%';

Insert image description here

You can see that the results include only those records that match the case exactly. When we use BINARY, mysql compares the data byte by byte. If BINARY is not used, data is compared verbatim.

 

Guess you like

Origin blog.csdn.net/WXF_Sir/article/details/131252159