English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
We can display the result as a comma-separated list with the help of the 'concat()' function that has the parameter ','.
Now let's look at an example. First, 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) - > );
mysql> insert into CommaSeperateDemo values(1,'John','Taylor'); mysql> insert into CommaSeperateDemo values(2,'Carol','Taylor'); mysql> insert into CommaSeperateDemo values(3,'Johnson','T.');
Now let's display all records.
mysql> select *FROM CommaSeperateDemo;
Here is the output.
+------+-----------+----------+ | Id | FirstName | LastName | +------+-----------+----------+ | 1 | John | Taylor | | 2 | Carol | Taylor | | 3 | Johnson | T. | +------+-----------+----------+ 3 rows in set (0.00 sec)
This is the syntax to get the result in the form of a comma-separated list under the help of the following command concat()
Select concat(ColumnName1, ',', ColumnName2, ',', ColumnName3,.............) as AliasName FROM yourTableName;
Now let's implement the above syntax in the following query.
mysql> SELECT concat(Id, ',', FirstName, ',', LastName) as CONCATEIDFIRSTANDLASTNAME FROM CommaSeperateDemo;
Here is the syntax.
+---------------------------+ | CONCATEIDFIRSTANDLASTNAME | +---------------------------+ | 1,John,Taylor | | 2,Carol,Taylor | | 3,Johnson,T. | +---------------------------+ 3 rows in set (0.00 sec)