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
Release: 2024-12-15 08:01:09
Original
380 people have browsed it

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

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

Example

Consider the following table:

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

If you execute the following query:

SELECT * FROM `table` WHERE `Value` = "iaresavage"
Copy after login
Copy after login

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

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!

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