Home >Database >Mysql Tutorial >How Can I Achieve Case-Sensitive Comparisons in MySQL?
When working with MySQL databases, it's important to consider the collation used for data storage. Collation defines the rules for data comparison, sorting, and case sensitivity. By default, MySQL uses case-insensitive collations, which may not always be appropriate.
If you require case-sensitive comparisons, you can specify the appropriate collation type during table creation or data insertion. According to the MySQL manual, you can set the collation to _cs to achieve case sensitivity.
To obtain a list of case-sensitive collations, execute the following query:
SHOW COLLATION WHERE COLLATION LIKE "%_cs"
However, further investigation reveals that MySQL currently does not support utf8_*_cs collations. If you need case sensitivity for utf8 fields, you should instead use utf8_bin. Keep in mind that this may affect ORDER BY operations. To resolve this, you can add COLLATE utf8_general_ci to the ORDER BY clause.
For example:
SELECT * FROM table_name ORDER BY column_name COLLATE utf8_general_ci;
The above is the detailed content of How Can I Achieve Case-Sensitive Comparisons in MySQL?. For more information, please follow other related articles on the PHP Chinese website!