Home >Database >Mysql Tutorial >How Do MySQL SELECT Queries Handle Case Sensitivity, and How Can I Enforce Case-Sensitive Comparisons?

How Do MySQL SELECT Queries Handle Case Sensitivity, and How Can I Enforce Case-Sensitive Comparisons?

Patricia Arquette
Patricia ArquetteOriginal
2024-12-15 08:01:09380browse

How Do MySQL SELECT Queries Handle Case Sensitivity, and How Can I Enforce Case-Sensitive Comparisons?

MySQL SELECT Queries: Case Sensitivity and Case Insensitivity

MySQL SELECT queries default to case-insensitive behavior, meaning that they do not distinguish between uppercase and lowercase characters. This means that the query you provided:

SELECT * FROM `table` WHERE `Value` = "iaresavage"

will match rows where the Value column contains either "iaresavage" or "IAREsAvagE".

Enforcing Case Sensitivity

If you want your query to be case sensitive, you can use a binary comparison operator, which explicitly specifies that the comparison should be made byte-by-byte:

SELECT * FROM `table` WHERE BINARY `Value` = "IAREsAvagE"

Example

Consider the following table:

CREATE TABLE `table` (`Value` VARCHAR(255));
INSERT INTO `table` VALUES ('iaresavage', 'IAREsAvagE');

If you execute the following query:

SELECT * FROM `table` WHERE `Value` = "iaresavage"

it will return both rows, even though the values are different.

However, if you execute the following query:

SELECT * FROM `table` WHERE BINARY `Value` = "IAREsAvagE"

it will only return the row where the value is exactly "IAREsAvagE".

The above is the detailed content of How Do MySQL SELECT Queries Handle Case Sensitivity, and How Can I Enforce Case-Sensitive Comparisons?. 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