JIYIK CN >

Current Location:Home > Learning > DATABASE > MySQL >

Check if string contains certain data in MySQL

Author:JIYIK Last Updated:2025/04/25 Views:

In this article, we aim to explore different ways to check the strings contained in a MySQL table.

We will introduce the following technologies in MySQL.

  • INSTRfunction
  • LOCATEfunction
  • LIKEOperators

Before we get started, however, we create a dummy dataset to work with. Here, we create a table, student_details, and a few rows in it.

-- create the table student_details
CREATE TABLE student_details(
  stu_id int,
  stu_firstName varchar(255) DEFAULT NULL,
  stu_lastName varchar(255) DEFAULT NULL,
  primary key(stu_id)
);
-- insert rows to the table student_details
INSERT INTO student_details(stu_id,stu_firstName,stu_lastName) 
 VALUES(1,"Preet","Sanghavi"),
 (2,"Rich","John"),
 (3,"Veron","Brow"),
 (4,"Geo","Jos"),
 (5,"Hash","Shah"),
 (6,"Sachin","Parker"),
 (7,"David","Miller");

The above query creates a table and rows in it containing the first and last name of the students. To see the entries in the data, we use the following code.

SELECT * FROM student_details;

The above code will give the following output.

stu_id	stu_firstName	stu_lastName
1	      Preet	        Sanghavi
2	      Rich	        John
3	      Veron	        Brow
4	      Geo	        Jos
5	      Hash	        Shah
6	      Sachin	    Parker
7	      David	        Miller

Let's aim to find all Parkerstudents whose last name contains the word .

Use the LOCATE function in MySQL to check if a string contains certain data in MySQL

The locate function in MySQL generally takes 2 parameters, for example LOCATE(substr, str). Here, substris the substring passed as the first parameter, and stris the string passed as the second parameter. LOCATEThe output of the function is the first row where the string passed as the parameter appears. To see this function in action, look at the code below.

-- finding the word 'Park' from the table where the last name of the student is Park.
SELECT * FROM student_details WHERE LOCATE('Park', stu_lastName) > 0 ;

The above code will give the following output:

stu_id	stu_firstName	stu_lastName
6	      Sachin	      Parker

Use the INSTR function in MySQL to check if a string in MySQL contains certain data

Similar to the LOCATE function, the INSTR function INSTR(str, substr) accepts 2 parameters. However, this function returns the index value of the first occurrence of the string within the substring passed in as a parameter. Here, strthe string is passed in as the first parameter, and substrthe substring is passed in as the second parameter. To see this function in action, look at the code below.

-- finding the word 'Park' from the table where the last name of the student is Park.
SELECT * FROM student_details WHERE INSTR(stu_lastName , 'Parker') > 0;

The above code will give the following output.

stu_id	stu_firstName	stu_lastName
6	      Sachin	      Parker

Check if a string in MySQL contains certain data using the LIKE operator in MySQL

Another alternative way to find if a string exists in the data is to use LIKE. This operator WHEREis used with the clause to find a specific string. To see this technique in action, look at the code below.

-- finding the word 'Park' from the table where the last name of the student is Parker.
SELECT * FROM student_details WHERE stu_lastName LIKE 'Parker' ;

The above code will again give the following output.

stu_id	stu_firstName	stu_lastName
6	      Sachin	      Parker

Additionally, %, also known as a wildcard character, is also used with the LIKE operator. As the name implies, this wildcard character represents none, one, or more characters. To see this wildcard character in action, look at the following code.

-- finding the student with last name ending in 'arker' from the table.
SELECT * FROM student_details WHERE stu_lastName LIKE '%arker' ;

The above code will again give the following output.

stu_id	stu_firstName	stu_lastName
6	      Sachin	      Parker

So, with the help of above three techniques, we can effectively find the presence of a string from the table.

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.

Article URL:

Related Articles

How to select from multiple tables in MySQL

Publish Date:2025/04/25 Views:57 Category:MySQL

This article explains how to use MySQL to query from multiple tables in one script SELECT . Let's demonstrate a situation: SELECT name , price, details, type , FROM food, food_order WHERE breakfast.id = 'breakfast_id' Now, let's imagine FRO

Creating a table from CSV in MySQL

Publish Date:2025/04/25 Views:114 Category:MySQL

In this article, we aim to understand how to create a table from CSV in MySQL database. Businesses and organizations must quickly generate tables from large amounts of data. These organizations typically have large CSV files with large amou

Creating a Temporary Table in MySQL

Publish Date:2025/04/25 Views:183 Category:MySQL

In this article, we aim to explore different ways to create temporary tables in MySQL. One of the main features of temporary tables is that it helps in storing temporary data. This feature is enabled in MySQL 3.23 and later versions. These

Truncate all tables in Mysql

Publish Date:2025/04/25 Views:89 Category:MySQL

Today I will show you how to truncate all tables in Mysql. It is used when you want to delete the entire table TRUNCATE TABLE . TRUNCATE It is a type of DML statement, which means it cannot be rolled back once it is committed. There are two

Different ways to check if a row exists in a MySQL table

Publish Date:2025/04/25 Views:163 Category:MySQL

This article highlights different ways to check if a row exists in a MySQL table. We will use the EXISTS and NOT EXISTS operators. We can also use these two operators with IF() the function to get a meaningful message if a row (a record) is

Check if table exists in MySQL

Publish Date:2025/04/25 Views:194 Category:MySQL

This article provides several options to check if a table exists in MySQL. Before discussing it, let us first see what a table is in MySQL and when you need to check its existence. What is a table in MySQL? A table is a database object that

Rename columns in MySQL database

Publish Date:2025/04/25 Views:80 Category:MySQL

In this article, we aim to explore different ways to rename columns in MySQL. ALTER TABLE The command is mainly used to change the format of a given MySQL table. It can be used to add columns, change the data type within a column, delete co

Copying a table in MySQL

Publish Date:2025/04/25 Views:142 Category:MySQL

The purpose of this article is to explore different ways to create a copy of a table in MySQL. The source table is also called the table to be copied, and the target table is called the clone table, which can be from the same or a different

Get column names in MySQL

Publish Date:2025/04/25 Views:109 Category:MySQL

In this article, we aim to explore how to get the column names of a specific table in MySQL database. Often, when working with data in MySQL, we tend to forget the column names of a particular table in the database and the data types of dif

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial