
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
MySQL query to check if a string contains a value (substring) within the same row?
Since we need to match strings from the same row, use LIKE operator. Let us first create a table −
mysql> create table DemoTable ( FirstName varchar(100), FullName varchar(100) ); Query OK, 0 rows affected (0.53 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('John','John Smith'); Query OK, 1 row affected (0.08 sec) mysql> insert into DemoTable values('David','John Miller'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('Bob','Sam Miller'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values('Chris','Chris Brown'); Query OK, 1 row affected (0.10 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+-----------+-------------+ | FirstName | FullName | +-----------+-------------+ | John | John Smith | | David | John Miller | | Bob | Sam Miller | | Chris | Chris Brown | +-----------+-------------+ 4 rows in set (0.00 sec)
Following is the query to check if a string contains a value within the same row −
mysql> select FirstName,FullName from DemoTable where FullName LIKE concat('%', FirstName, '%');
This will produce the following output −
+-----------+-------------+ | FirstName | FullName | +-----------+-------------+ | John | John Smith | | Chris | Chris Brown | +-----------+-------------+ 2 rows in set (0.00 sec)
- Related Articles
- MySQL query to check if a string contains a word?
- Java Program to Check if a string contains a substring
- Golang program to check if a string contains a substring
- Check if a String Contains a Substring in Linux
- How to check if a string contains a substring in Golang?
- PHP 8 – Using str_contains() to check if a string contains a substring
- Check if a string contains numbers in MySQL?
- How to check if an input string contains a specified substring in JSP?
- Check if a string is entirely made of the same substring JavaScript
- SELECT where row value contains string in MySQL?
- How to check whether a string contains a substring in JavaScript?
- How to check whether a string contains a substring in jQuery?
- How to check whether a String contains a substring or not?
- MySQL query to check if a certain row has only two words?
- How do we check if a String contains a substring (ignoring case) in Java?

Advertisements