- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to get MySQL combined field result?
You can use CONCAT() function from MySQL for this. Let us first create a table −
mysql> create table DemoTable ( ClientId int NOT NULL AUTO_INCREMENT PRIMARY KEY, ClientFirstName varchar(20), ClientLastName varchar(20) ); Query OK, 0 rows affected (0.50 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable(ClientFirstName,ClientLastName) values('John','Smith'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable(ClientFirstName,ClientLastName) values('John','Miller'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable(ClientFirstName,ClientLastName) values('Carol','Taylor'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable(ClientFirstName,ClientLastName) values('Adam','Smith'); 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 −
+----------+-----------------+----------------+ | ClientId | ClientFirstName | ClientLastName | +----------+-----------------+----------------+ | 1 | John | Smith | | 2 | John | Miller | | 3 | Carol | Taylor | | 4 | Adam | Smith | +----------+-----------------+----------------+ 4 rows in set (0.00 sec)
Following is the query to get combined field result −
mysql> select ClientId from DemoTable where concat(ClientFirstName,' ',ClientLastName) LIKE 'Jo%';
This will produce the following output −
+----------+ | ClientId | +----------+ | 1 | | 2 | +----------+ 2 rows in set (0.00 sec)
Advertisements