ใช้ GROUP_CONCAT() รวมค่าคอลัมน์หลายแถวเป็นสตริงคั่นด้วยคอมม่า
สร้างตาราง (AUTO_INCREMENT เริ่มที่ 101)
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)
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)
คิวรีรวมเป็น 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)
ตัวเลือกเพิ่มเติม: GROUP_CONCAT(col SEPARATOR ';') เปลี่ยนตัวคั่น, ORDER BY col เรียงก่อนรวม