Searching rows for a substring in MySQL
In some cases, you must search for a string or substring in a table.
For example, you want to know employee
how many Gmail users there are in the table. Another example is employee
to find all Je
the names starting with in the table firstname
.
Here, searching for a string or substring comes into the picture, and there are different ways to search for it: POSITION()
for standard SQL, LOCATE()
and with %
wildcards for MySQL, and with String INSTR()
for Oracle.
We will learn the methods supported by MySQL and observe which one is the best and simple way to search rows for a substring in MySQL. You can look at all these methods and decide which one is best for your situation.
Method 1: Use %
wildcards to find strings/substrings
%
A wildcard character is used to replace one or more characters in a string. This wildcard character LIKE
is used with the operator, LIKE
which is used in WHERE
a clause.
For the purpose of practicing, we have created a employee
new table named . You can create it and fill it with data using the sample code given below.
This way, we will all be in step as we learn.
#Create employee table
CREATE TABLE employee(
id int not null primary key,
firstname varchar(60) not null,
lastname varchar(60) not null,
gender varchar(30) not null,
city varchar(40) not null,
email varchar(60) not null
);
#Insert data into employee table.
INSERT INTO employee VALUES
(1,'James','Robert','Male','London','jrober@gmail.com'),
(2,'Mary','Patricia','Female','Berlin','patricia@gmail.com'),
(3,'John','Michael','Male','Paris','johnmichael@yahoo.com'),
(4,'Jennifer','Linda','Female','Lisbon','jennifer@hotmail.com'),
(5,'William','David','Male','Rome','wdwilliam@gmail.com'),
(6,'Elizabeth','Barbara','Female','Dublin','elibarbara011@yahoo.com'),
(7,'Richard','Joseph','Male','Oslo', 'richard@gmail.com'),
(8,'Susan','Jessica','Female','Hamburg','susan01@yahoo.com'),
(9,'Thomas','Charles','Male','Texas', 'thomas.charles@hotmail.com'),
(10,'Karen','Nancy','Female','Washington','karenofficial@gmail.com');
SELECT * FROM employee;
Output:
Let us understand %
different scenarios considering searching lines for a string or a substring using wildcard characters.
Scenario 1: Find all Gmail
users. That means, we have to email
find a substring in .
SELECT firstname, lastname from employee where email LIKE '%@gmail.com';
The above SQL query will select and from the table email
where the string gmail.com
contains .employee
firstname
lastname
Output:
Scenario 2: Select a
last names ending with letters . Here we will find a string lastname
.
SELECT lastname FROM employee WHERE lastname LIKE '%a';
Output:
Scenario 3: Let's find names that L
start with and n
end with city
, with four characters in between. An underscore ( _
) represents one character.
SELECT city FROM employee WHERE city LIKE 'L____n';
Output:
You can learn more about wildcards here.
Method 2: Use LOCATE()
the function to find a string/substring
LOCATE
The function returns the first occurrence of the substring in the string. If the substring cannot be found in the original string, it returns 0
.
This function performs a case-insensitive search, which means HELLO
, same hello
as LOCATE
the function. It is used with the following syntax.
LOCATE(substring, string, start)
In the syntax given above, the first two parameters named substring and string are required, but start
the third parameter named is optional.
You might be wondering how the output will look like? Let’s understand with the following image.
SELECT LOCATE("hotmail", email) FROM employee;
Output:
in conclusion
We conclude that different database platforms support different methods. LOCATE()
,Wildcards are compatible with MySQL.
We have seen the methods supported by MySQL to search for a string or substring with the help of examples and observed the output to understand.
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
Changing max_allowed_packet Size in MySQL Server
Publish Date:2025/04/22 Views:192 Category:MySQL
-
This article explains how to change the max_allowed_packet size in MySQL server. To understand this, we will use two operating systems, Windows 10 and Linux (Ubuntu). Changing max_allowed_packet Size in MySQL Server If we try to upload a fi
Zerofill usage, advantages and alternatives in MySQL
Publish Date:2025/04/22 Views:195 Category:MySQL
-
In this article we will understand the uses, advantages and alternatives of ZEROFILL attribute in MySQL. Use and benefits of the ZEROFILL attribute in MySQL The benefit of using the ZEROFILL attribute is that it has nothing to do with input
Compare only MySQL timestamp dates to date parameters
Publish Date:2025/04/22 Views:64 Category:MySQL
-
In this article we will use the DATE() , CAST() , and CONVERT() functions to compare MySQL timestamp dates with only the date parameter. DATE() vs. CAST() vs. CONVERT() in MySQL Below is a brief description of each function. You can also fi
Calculating Percentages in MySQL
Publish Date:2025/04/22 Views:66 Category:MySQL
-
We will use one or more columns to calculate percentages in MySQL. There are different ways to do this, and for each method we will use an example table. Calculate percentage using a column in MySQL We have a table called sales where ID, Re
Selecting multiple values using WHERE in MySQL
Publish Date:2025/04/22 Views:185 Category:MySQL
-
This article is about using MySQL query to get data from a specific table or relation that satisfies a specific condition. To do this, the WHERE clause is used in the SQL query. WHERE clause in SQL query WHERE The clause specifies the condi
Changing the connection timeout in MySQL
Publish Date:2025/04/22 Views:59 Category:MySQL
-
We are learning how to change the connection timeout in MySQL using Linux (Ubuntu 20.04) and Windows operating systems. Changing the connection timeout in MySQL Sometimes you keep losing connection to the MySQL server because the connect_ti
MySQL fix Data Is Truncated for a Column error
Publish Date:2025/04/22 Views:101 Category:MySQL
-
This article describes possible causes and solutions for the MySQL error Data is truncated for a column . Fix data truncated due to column error in MySQL Here, we will discuss the possible causes and solutions to eliminate MySQL data trunca
MySQL Error Server PID File Could Not Be Found Solution
Publish Date:2025/04/22 Views:192 Category:MySQL
-
In this article, we will study about the Error! Error Server PID File Could Not Be Found! in MySQL and its solution with full explanation. MySQL PID file The file that contains the process identification number or process ID of a running My
Get the last inserted ID using PHP MySQLi function
Publish Date:2025/04/22 Views:99 Category:MySQL
-
This article briefly introduces the PHP mysqli() function and demonstrates how to use it to get the last inserted ID from a MySQL database. PHP mysqli() Function It is an extended version of the MySQL driver called mysqli and is typically u