
- 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
How to use user variables in MySQL LIKE clause?
Using the CONCAT() function, we can work with user variables in LIKE clause. The syntax is as follows.
set @anyVariableName='anyValue'; select yourColumnName1,yourColumnName2,yourColumnName3,...N from yourTableName whereyourColumnName like CONCAT('%', @anyVariableName, '%');
To understand the above syntax, let us first create a table. The query to create a table is as follows.
mysql> create table UserVariableInLike -> ( -> id int, -> Name varchar(100), -> Age int -> ); Query OK, 0 rows affected (0.83 sec)
Insert records in the table using insert command. The query is as follows.
mysql> insert into UserVariableInLike values(101,'John',23); Query OK, 1 row affected (0.23 sec) mysql> insert into UserVariableInLike values(102,'John Smith',24); Query OK, 1 row affected (0.20 sec) mysql> insert into UserVariableInLike values(103,'Carol Smith',23); Query OK, 1 row affected (0.15 sec) mysql> insert into UserVariableInLike values(104,'Johnson',25); Query OK, 1 row affected (0.20 sec) mysql> insert into UserVariableInLike values(105,'Adam Smith',26); Query OK, 1 row affected (0.21 sec)
Display all records from the table using select statement. The query is as follows
mysql> select *from UserVariableInLike;
The following is the output.
+------+-------------+------+ | id | Name | Age | +------+-------------+------+ | 101 | John | 23 | | 102 | John Smith | 24 | | 103 | Carol Smith | 23 | | 104 | Johnson | 25 | | 105 | Adam Smith | 26 | +------+-------------+------+ 5 rows in set (0.00 sec)
Here is the query which uses the user variable in the LIKE clause. The query is as follows −
mysql> set @searchName='John'; Query OK, 0 rows affected (0.00 sec) mysql> select id,Name,Age from UserVariableInLike where Name like CONCAT('%', @searchName, '%');
The following is the output.
+------+------------+------+ | id | Name | Age | +------+------------+------+ | 101 | John | 23 | | 102 | JohnSmith | 24 | | 104 | Johnson | 25 | +------+------------+------+ 3 rows in set (0.05 sec)
- Related Articles
- How to use MySQL LIKE clause to fetch multiple values beginning with “Joh”
- Can we use MySQL GROUP BY clause with multiple columns like MySQL DISTINCT clause is used?
- How Can MySQL GROUP BY clause behave like DISTINCT clause?
- User-defined variables vs Local Variables in MySQL?
- System variables vs User-defined variables in MySQL?
- Using User-Defined Variables in MySQL
- How to use MySQL VIEW with WHERE clause?
- Using LIKE clause twice in a MySQL query
- How to use union and order by clause in MySQL?
- How to use MySQL Date functions with WHERE clause?
- How to use MySQL DISTINCT clause on multiple columns?
- How can user variables be used in MySQL stored procedure?
- Display different variables in MySQL using LIKE?
- How Can we use MySQL DISTINCT clause with WHERE and LIMIT clause?
- How can we use MySQL SELECT without FROM clause?

Advertisements