我们可以在带有参数','的'concat()'函数的帮助下,将结果显示为逗号分隔的列表。
现在让我们来看一个例子。首先,我们将创建一个表。CREATE命令用于创建表。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.');
现在让我们显示所有记录。mysql> select *from CommaSeperateDemo;
以下是输出。+------+-----------+----------+
| Id | FirstName | LastName |
+------+-----------+----------+
| 1 | John | Taylor |
| 2 | Carol | Taylor |
| 3 | Johnson | T. |
+------+-----------+----------+
3 rows in set (0.00 sec)
这是在以下命令的帮助下以逗号分隔列表的形式获取结果的语法 concat()Select concat(ColumnName1, ',', ColumnName2, ',', ColumnName3,.............) as AliasName from yourTableName;
现在让我们在以下查询中实现以上语法。mysql> SELECT concat(Id, ',', FirstName,',', LastName) as CONCATEIDFIRSTANDLASTNAME from CommaSeperateDemo;
以下是语法。+---------------------------+
| CONCATEIDFIRSTANDLASTNAME |
+---------------------------+
| 1,John,Taylor |
| 2,Carol,Taylor |
| 3,Johnson,T. |
+---------------------------+
3 rows in set (0.00 sec)