如何以 CSV 表示 MySQL 中的列值?
要以 CSV 形式显示列值,请使用 GROUP_CONCAT()。
我们首先创建一个表 −
mysql> create table DemoTable786 ( StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY, StudentName varchar(100) ) AUTO_INCREMENT=101; Query OK, 0 rows affected (0.70 sec)
使用 insert 命令在表中插入一些记录 −
mysql> insert into DemoTable786(StudentName) values('Chris');
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable786(StudentName) values('Robert');
Query OK, 1 row affected (0.24 sec)
mysql> insert into DemoTable786(StudentName) values('Mike');
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable786(StudentName) values('Sam');
Query OK, 1 row affected (0.12 sec)使用 select 语句从表中显示所有记录 −
mysql> select *from DemoTable786;
这将产生以下输出 -
+-----------+-------------+ | StudentId | StudentName | +-----------+-------------+ | 101 | Chris | | 102 | Robert | | 103 | Mike | | 104 | Sam | +-----------+-------------+ 4 rows in set (0.00 sec)
以下是在 MySQL 中选择列以 CSV 形式显示的查询 −
mysql> select group_concat(StudentId),group_concat(StudentName) from DemoTable786;
这将产生以下输出 -
+-------------------------+---------------------------+ | group_concat(StudentId) | group_concat(StudentName) | +-------------------------+---------------------------+ | 101,102,103,104 | Chris,Robert,Mike,Sam | +-------------------------+---------------------------+ 1 row in set (0.00 sec)
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言
C++
C#
MongoDB
MySQL
Javascript
PHP