Home >Database >Mysql Tutorial >How Can I Perform a Case-Sensitive Query in MySQL?

How Can I Perform a Case-Sensitive Query in MySQL?

Patricia Arquette
Patricia ArquetteOriginal
2024-12-06 17:04:12996browse

How Can I Perform a Case-Sensitive Query in MySQL?

MySQL Case-Sensitive Query

In MySQL, queries are case-insensitive by default. However, you can enforce case-sensitive comparisons to ensure that your results match the exact spelling and capitalization of the specified criteria.

Problem:

When querying a table with a 'Location' column, you want to retrieve only rows where the location value exactly matches 'San Jose' (case-sensitive). By default, the query below would return results with 'san jose' or any other variation.

SELECT Seller FROM Table WHERE Location = 'San Jose'

Solution:

To perform a case-sensitive comparison, use the BINARY operator before the column name in your query:

SELECT Seller FROM Table WHERE BINARY Location = 'San Jose'

By using BINARY, you instruct MySQL to perform a byte-by-byte comparison, ignoring any case differences. As a result, the query will only return rows where the 'Location' column contains the exact value 'San Jose'.

Note: This case-sensitive comparison is particularly useful when searching for data in columns that contain mixed case or special characters, such as user names or passwords.

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