
- 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
Group concatenate the last name from a MySQL column and set a condition to display limited records
Use GROUP_CONCAT() to perform group concatenation and set a condition. Let us first create a table −
mysql> create table DemoTable813( UserId int NOT NULL AUTO_INCREMENT PRIMARY KEY, UserFirstName varchar(100), UserLastName varchar(100) ); Query OK, 0 rows affected (0.49 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable813(UserFirstName,UserLastName) values('Adam','Smith'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable813(UserFirstName,UserLastName) values('Chris','Brown'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable813(UserFirstName,UserLastName) values('David','Miller'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable813(UserFirstName,UserLastName) values('Carol','Taylor'); Query OK, 1 row affected (0.22 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable813;
This will produce the following output −
+--------+---------------+--------------+ | UserId | UserFirstName | UserLastName | +--------+---------------+--------------+ | 1 | Adam | Smith | | 2 | Chris | Brown | | 3 | David | Miller | | 4 | Carol | Taylor | +--------+---------------+--------------+ 4 rows in set (0.00 sec)
Following is how to group concatenate and display only the last names −
mysql> select group_concat(UserLastName) from DemoTable813 where UserId < 4;
This will produce the following output −
+----------------------------+ | group_concat(UserLastName) | +----------------------------+ | Smith,Brown,Miller | +----------------------------+ 1 row in set (0.00 sec)
- Related Articles
- Group by one column and display corresponding records from another column with a separator in MySQL
- MySQL GROUP BY and CONCAT() to display distinct first and last name
- MySQL query to display the count of distinct records from a column with duplicate records
- Add a column count in a MySQL query on the basis of last name records?
- Display and concatenate records ignoring NULL values in MySQL
- MySQL query to display columns name first name, last name as full name in a single column?
- Display records where first and last name begins with the same letter in MySQL
- How to compare the first date and the last date from a MySQL column with date records?
- Display month names and year from a column with date records with MySQL
- Display distinct dates in MySQL from a column with date records
- Display the count of duplicate records from a column in MySQL and order the result
- Concatenate the column values with separate text in MySQL and display in a single column
- MySQL query to select everything to left of last space in a column with name records
- How to group by column name and ensure the query retrieves the last update in MySQL?
- How to display first day and last day of the month from date records in MySQL?

Advertisements