How to get MySQL combined field result?

MySQLMySQLi Database

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)
raja
Published on 21-May-2019 07:50:40
Advertisements