
- 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
Display MySQL Results as comma separated list?
We can show the result as a comma separated list with the help of the ‘concat()’ function with the parameter ‘,’.
Let us see an example now. Firstly, we will create a table. The CREATE command is used to create a table.
mysql> create table CommaSeperateDemo - > ( - > Id int, - > FirstName varchar(100), - > LastName varchar(100) - > ); Query OK, 0 rows affected (0.93 sec)
Inserting records
mysql> insert into CommaSeperateDemo values(1,'John','Taylor'); Query OK, 1 row affected (0.17 sec) mysql> insert into CommaSeperateDemo values(2,'Carol','Taylor'); Query OK, 1 row affected (0.12 sec) mysql> insert into CommaSeperateDemo values(3,'Johnson','T.'); Query OK, 1 row affected (0.16 sec)
Let us now display all the records.
mysql> select *from CommaSeperateDemo;
The following is the output.
+------+-----------+----------+ | Id | FirstName | LastName | +------+-----------+----------+ | 1 | John | Taylor | | 2 | Carol | Taylor | | 3 | Johnson | T. | +------+-----------+----------+ 3 rows in set (0.00 sec)
Here is the syntax to get the result as comma separated list with the help of concat()
Select concat(ColumnName1, ',', ColumnName2, ',', ColumnName3,.............) as AliasName from yourTableName;
Let us now implement the above syntax in the following query.
mysql> SELECT concat(Id, ',', FirstName,',', LastName) as CONCATEIDFIRSTANDLASTNAME from CommaSeperateDemo;
The following is the syntax.
+---------------------------+ | CONCATEIDFIRSTANDLASTNAME | +---------------------------+ | 1,John,Taylor | | 2,Carol,Taylor | | 3,Johnson,T. | +---------------------------+ 3 rows in set (0.00 sec)
- Related Articles
- Display duplicate record as a distinct value with corresponding values as distinct comma separated list in MySQL?
- How to set a comma separated list as a table in MySQL?
- How to display data from a MySQL column separated by comma?
- Check if value exists in a comma separated list in MySQL?
- Display all the column values in a single row separated by comma in MySQL?
- Searching from a comma separated MySQL column?
- Convert String into comma separated List in Java
- MySQL query to retrieve records from the part of a comma-separated list?
- How can I match a comma separated list against a value in MySQL?
- Regex to find string and next character in a comma separated list - MySQL?
- Count values from comma-separated field in MySQL?
- Get values from all rows and display it a single row separated by comma with MySQL
- Comma separated argument applicable for IN operator in MySQL?
- Fetch records from comma separated values using MySQL IN()?
- How to turn JavaScript array into the comma-separated list?

Advertisements