Home >Database >Mysql Tutorial >How Can I Achieve Case-Sensitive Comparisons in MySQL?

How Can I Achieve Case-Sensitive Comparisons in MySQL?

DDD
DDDOriginal
2024-11-28 07:37:14469browse

How Can I Achieve Case-Sensitive Comparisons in MySQL?

Case Sensitivity in MySQL Collation

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn