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

How Can I Perform Case-Sensitive String Comparisons in MySQL?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2025-01-21 07:07:101056browse

How Can I Perform Case-Sensitive String Comparisons in MySQL?

Achieving Case-Sensitive String Comparisons in MySQL

Problem: MySQL's default string comparison is case-insensitive. How can we perform case-sensitive comparisons?

Solution:

Utilize the BINARY keyword within your MySQL queries to enforce case sensitivity. For instance:

<code class="language-sql">SELECT * FROM `your_table` WHERE BINARY `your_column` = 'your_value';</code>

By prepending BINARY to the column name, MySQL interprets the column as a binary string, resulting in a case-sensitive comparison. This accurately differentiates strings with varying capitalization.

Illustrative Example:

Consider this case-insensitive comparison:

<code class="language-sql">SELECT * FROM `your_table` WHERE `your_column` = 'Example';</code>

This query retrieves rows where your_column contains 'Example', 'example', 'EXAMPLE', etc.

Now, let's introduce case sensitivity:

<code class="language-sql">SELECT * FROM `your_table` WHERE BINARY `your_column` = 'Example';</code>

This modified query only returns rows where your_column precisely matches 'Example' – case matters!

The above is the detailed content of How Can I Perform Case-Sensitive String 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