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
Release: 2025-01-21 07:07:10
Original
1033 people have browsed it

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>
Copy after login

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>
Copy after login

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>
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template