Home > Database > Mysql Tutorial > body text

How can I achieve case-insensitive search with LIKE operator in MySQL?

Barbara Streisand
Release: 2024-11-09 03:48:02
Original
240 people have browsed it

How can I achieve case-insensitive search with LIKE operator in MySQL?

MySQL Queries: Case Sensitivity Conundrum

When working with MySQL queries, it's crucial to consider case sensitivity to ensure accurate search results. However, a common issue arises when using the LIKE operator with utf8_general_ci encoding, leading to case-sensitive search behavior.

To address this, there are several solutions that can preserve case-insensitivity:

1. Binary String Comparison:

SELECT .... FROM .... WHERE `concatenated` LIKE BINARY '%SearchTerm%';
Copy after login

By using BINARY, the string comparison becomes case-insensitive, as binary strings are treated as a sequence of bytes without regard to character case.

2. COLLATE Clause with utf8_bin:

SELECT ....
FROM ....
WHERE `concatenated` like '%SearchTerm%' COLLATE utf8_bin;
Copy after login

The COLLATE clause specifies a specific character set and collation for the comparison. utf8_bin ensures case-insensitive comparisons for utf8 data.

Performance Considerations:

Note that using BINARY or COLLATE can impact query performance. BINARY comparisons can be more efficient, while COLLATE can introduce additional processing overhead. Therefore, the optimal solution may depend on the specific usage scenario.

The above is the detailed content of How can I achieve case-insensitive search with LIKE operator 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