

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
MySQL GROUP BY and CONCAT() to display distinct first and last name
Let us first create a table −
mysql> create table DemoTable ( FirstName varchar(100), LastName varchar(100) ); Query OK, 0 rows affected (0.92 sec) mysql> alter table DemoTable add index(FirstName,LastName); Query OK, 0 rows affected (1.00 sec) Records: 0 Duplicates: 0 Warnings: 0
Insert some records in the table using insert command −
mysql> insert into DemoTable values('Adam','Smith'); Query OK, 1 row affected (0.73 sec) mysql> insert into DemoTable values('Adam','Smith'); Query OK, 1 row affected (1.17 sec) mysql> insert into DemoTable values('John','Doe'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values('Carol','Taylor'); Query OK, 1 row affected (0.25 sec) mysql> insert into DemoTable values('John','Doe'); Query OK, 1 row affected (0.66 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+-----------+----------+ | FirstName | LastName | +-----------+----------+ | Adam | Smith | | Adam | Smith | | Carol | Taylor | | John | Doe | | John | Doe | +-----------+----------+ 5 rows in set (0.00 sec)
Following is the query to combine distinct First and Last Name −
mysql> select concat(FirstName,' ',LastName) as combinedName from DemoTable group by LastName,FirstName;
This will produce the following output −
+--------------+ | combinedName | +--------------+ | Adam Smith | | Carol Taylor | | John Doe | +--------------+ 3 rows in set (0.00 sec)
- Related Questions & Answers
- MySQL query to group concat distinct by Id?
- Getting last value in MySQL group concat?
- Split First name and Last name using JavaScript?
- Display records where first and last name begins with the same letter in MySQL
- Display distinct column name in MySQL
- How to group by column name and ensure the query retrieves the last update in MySQL?
- MySQL query to display columns name first name, last name as full name in a single column?
- Group concatenate the last name from a MySQL column and set a condition to display limited records
- Display records by first fixing the first two values in a column and then using DISTINCT to display other values in MySQL
- SELECT DISTINCT vs GROUP BY in MySQL?
- Validate the first name and last name with Java Regular Expressions
- Constructing full name from first name, last name and optional middle name in JavaScript
- GROUP BY and display only non-empty column values in MySQL
- MySQL- GROUP and COUNT by date?
- Using GROUP BY and COUNT in a single MySQL query to group duplicate records and display corresponding max value
Advertisements