MySQL using LIKE in case insensitive search
This tutorial article will show you how to use LIKE
the operator to search a column. We will show you how to use it correctly to include all results that meet certain criteria, regardless of whether the value is uppercase or lowercase.
LIKE
Use case-insensitive search in MySQL
When searching a table, always WHERE
use LIKE
the operator after the .
This is particularly useful when searching for a term you're unsure of, such as a prefix or letter pattern, or if you want to include multiple instances that meet certain criteria.
After SELECT
the statement, to specify which column in the table to search, add LIKE
the operator, followed by the following wildcard characters:
%
The wildcard character will find values starting with the letter that comes after the letter. Likewise, if%
the symbol is placed before the letter, it will find values ending with the letter.- The underscore (
_
) wildcard character is used to%
limit the search to one value after the symbol.
You can use the above in different ways to produce different results. The following example shows how to use two wildcards to search for a term.
CREATE TABLE car_models (
brand TEXT NOT NULL,
model TEXT NOT NULL
);
INSERT INTO car_models VALUES ('Toyota', 'Camry');
INSERT INTO car_models VALUES ('toyota', 'Corolla');
INSERT INTO car_models VALUES ('toyota', 'corolla');
INSERT INTO car_models VALUES ('Ford', 'Prefect');
INSERT INTO car_models VALUES ('Ford', 'fiesta');
SELECT * FROM car_models WHERE car_models.model LIKE '%cor%'
The above will only return rows that contain the letters in that sequence cor
. By %
starting and ending with the symbol, the search is limited to words that contain the letters in that order.
| brand | model |
|-------|---------|
|toyota | Corolla |
|toyota | corolla |
The search ignores case and searches only for values that match the criteria. You can also run the following statement, which returns c
all models that contain the letters .
SELECT * FROM car_models WHERE car_models.model LIKE '%C%'
Output:
| brand | model |
|-------|---------|
|Toyota | Camry |
|toyota | Corolla |
|toyota | corolla |
|Ford | Prefect |
The following returns all c
models that begin with the letters . However, if you want to be more specific, you can c
place %
a symbol after just the letters to return c
models that begin with .
SELECT * FROM car_models WHERE car_models.model LIKE 'C%'
Output:
| brand | model |
|-------|---------|
|Toyota | Camry |
|toyota | Corolla |
|toyota | corolla |
Case-sensitive search in MySQL
If you want to do a case-sensitive search, you can LIKE
add before the operator BINARY
and the search will be case-sensitive.
SELECT * FROM car_models WHERE car_models.model LIKE BINARY 'c%'
Output:
| brand | model |
|-------|---------|
|toyota | corolla |
All models beginning with an C
uppercase are excluded from the search.
Regular expressions in MySQL
It's worth learning how to use regular expressions to do more specific searches. These include more special characters, such as %
the ampersand, allowing you to do more in your searches.
It *
consists of the symbol, which allows the following situation, that the value following it can appear multiple times or not at all. On the other hand, +
the sign indicates that the following character needs to appear at least once.
REGEXP
and REGEXP_LIKE()
can also be used in MySQL to perform specific patterns.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Loop PHP MySQLi Get Array Function
Publish Date:2025/04/21 Views:105 Category:MySQL
-
MySQLi fetch function is used to access data from the database server. After fetching the data, you can also iterate over it MySQLi with queries. In this article, we will see mysqli_fetch_array() the use of functions and methods to iterate
Generate a random and unique string in MySQL
Publish Date:2025/04/21 Views:77 Category:MySQL
-
Today, we will learn to use various functions in MySQL to generate random and unique strings. These functions include MD5() , RAND() , , SUBSTR() and UUID() . There is no inbuilt method to generate random string in MySQL, but there are many
Changing MySQL root password on Mac
Publish Date:2025/04/21 Views:160 Category:MySQL
-
This article teaches you how to change the MySQL user password on OS X. root We will be using XAMPP so that you can change the password using the MySQL console. Installing XAMPP for OSX First, download and install XAMPP for OSX from Apache
Using CURRENT_TIMESTAMP as a default value in MySQL
Publish Date:2025/04/21 Views:196 Category:MySQL
-
This article teaches you how to use as 5.6.5 in MySQL versions lower than . Thus, you can prevent MySQL error 1293. CURRENT_TIMESTAMP DEFAULT Our approach involves reordering table columns and using DEFAULT 0 and time values. Reproduce the
If ELSE in MySQL
Publish Date:2025/04/11 Views:86 Category:MySQL
-
In this tutorial, we aim to explore how to use IF ELSE the statement in MySQL. One of the key roles of a data analyst is to gather insights from the data and produce meaningful results. It can be done with the help of several data filtering
DATETIME vs. TIMESTAMP in MySQL
Publish Date:2025/04/11 Views:117 Category:MySQL
-
DATETIME and TIMESTAMP are two different data types that can be used to store values that must contain both a date and a time portion. In this article, we will understand how it is stored in the database and the memory required for ea
Execute multiple joins in one query in MYSQL
Publish Date:2025/04/11 Views:94 Category:MySQL
-
Have you ever wondered how to include multiple joins in one query in MySQL? You have come to the right place. Remember that joins allow us to access information from other tables. This information is included separately to avoid redundancy.
Joining 3 tables in MySQL
Publish Date:2025/04/11 Views:187 Category:MySQL
-
In this tutorial, we will learn how to join three tables in MySQL. Businesses and organizations may have to visualize three tables simultaneously based on certain matching columns common to all three tables. This operation is allowed in MyS
Use of UPDATE JOIN in MySQL
Publish Date:2025/04/11 Views:85 Category:MySQL
-
This tutorial will explain how to use the statement in MySQL database UPDATE JOIN . We generally use joins to iterate over the rows in a particular table which may or may not have similar rows in other tables. We can UPDATE use JOIN the cla