MySQL MariaDB

MySQL Select Where String Contains

When you have a MySQL database, you may run into a case where you want to make a specific “select” command that includes selecting a string that contains a given search term. If your goal is to find a given phrase on your database, you must know how to select the values where the string is contained.

The good news is that with MySQL, you can use the LIKE clause combined with the % wildcard to select a given phrase in your “select” query. This post details on how to select the values where the string is contained using MySQL. Let’s dig in!

Understanding How to Select the Values Where the String Is Contained in MySQL

When you have your database, you can use the LIKE and the % wildcard to select the values where the string is contained in three different instances. You can select all values that contain the specified string. Still, you can tune the “select” query only to display those that start or end with the specified string. We will detail all the instances, but let’s create our sample database first as follows:

Let’s create our database named “student” with the following command:

CREATE DATABASE <database-name>;

Next, create a sample table in the database and insert different values as shown in the following:

Inspect the table to see the present values. In our table, we have three columns. We focus our “select” query on the “name” and “course” columns as they contain the string values.

With that, let’s now detail how you can use MySQL to select the values where a string is contained in three different ways. Take a look!

1. Select the Values that Have the Substring

The first way to select the values where the string is contained is by finding all values in the specified column that contain the specified phrase. You can select the values with the entire phrase as a substring or whose substring contains a section of the specified phrase.

For our first example, let’s select all strings that contain “Terry” in the “name” column. For that, we type our command as follows:

SELECT * FROM details WHERE name LIKE ‘%Terry%’;

Note how and where we added the LIKE and the % wildcard in the query. Here, we specify that the “select” statement should find all values that contain the specified substring.

Suppose you don’t know the exact “search” query but you know a section of characters that it contains. You can also use the same “select” query to match all values with the specified string section. For instance, the following query finds all courses that have “ech” in their name.

SELECT * FROM details WHERE course LIKE ‘%ech%’;

You can tune the search phrase to your need to ensure that you meet your search goal.

2. Select the Values that Start with the Substring

Similar to how we searched for all values that contain our search substring, we can also find the values that start with the search substring. It could be that you are unsure of the exact substring that you want to find but you know that it starts with a given phrase.

In that case, you only include the % wildcard at the end of the search phrase.

Here’s an example. We want to find all names that start with “ki”. Our “select” query is as follows:

SELECT * FROM details WHERE name LIKE ‘ki%’;

The previous results confirm that selecting where the string is contained worked as expected.

3. Select the Values that End with the Substring

The last method to select the values where the string is contained is by checking all values that end with the specified phrase. Unlike how we add the % wildcard at the end of the search phrase, we add the % wildcard here at the start of the phrase.

Here’s an example to find all courses with “ing” at the end of their name.

Conclusion

With MySQL, you can select the values where the string is contained in three ways. You only need to utilize the LIKE phrase and the % wildcard in your query to specify how you want your “select” query to be. This post discussed the three ways of selecting the values where the string is contained with given examples for each case. That’s it!

About the author

Denis Kariuki

Denis is a Computer Scientist with a passion for Networking and Cyber Security. I love the terminal, and using Linux is a hobby. I am passionate about sharing tips and ideas about Linux and computing.