
- 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
Combine columns before matching it with LIKE in a single query in MySQL?
You can use CONCAT() function for this. Let us first create a table −
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, Value1 varchar(10), Value2 varchar(10) ); Query OK, 0 rows affected (0.21 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable(Value1,Value2) values('10','345'); Query OK, 1 row affected (0.07 sec) mysql> insert into DemoTable(Value1,Value2) values('14','789'); Query OK, 1 row affected (0.06 sec) mysql> insert into DemoTable(Value1,Value2) values('18','234'); Query OK, 1 row affected (0.13 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+----+--------+--------+ | Id | Value1 | Value2 | +----+--------+--------+ | 1 | 10 | 345 | | 2 | 14 | 789 | | 3 | 18 | 234 | +----+--------+--------+ 3 rows in set (0.00 sec)
Following is the query to combine columns before matching it with LIKE in a single query −
mysql> SELECT * FROM DemoTable WHERE CONCAT(Value1, Value2) LIKE '%147%';
This will produce the following output −
+----+--------+--------+ | Id | Value1 | Value2 | +----+--------+--------+ | 2 | 14 | 789 | +----+--------+--------+ 1 row in set (0.00 sec)
- Related Articles
- MySQL query to combine two columns in a single column?
- Update two columns with a single MySQL query
- Change multiple columns in a single MySQL query?
- Implement multiple LIKE operators in a single MySQL query
- MySQL query to sort multiple columns together in a single query
- Count two different columns in a single query in MySQL?
- Implement SELECT LIKE and CHAR_LENGTH() in a single MySQL query
- Use LIKE % to fetch multiple values in a single MySQL query
- Display substrings of different length with a single MySQL query and combine the result
- Combine columns into rows with MySQL?
- How to combine different columns of a table to yield a single column in query output in PostgreSQL?
- Order by a function of two columns in a single MySQL query
- Can we implement 'LIKE' and ‘IN’ in a single MySQL query?
- Comparing two columns in a single MySQL query to get one row?
- How to use MySQL LIKE query to search a column value with % in it?

Advertisements