ใช้ GROUP_CONCAT() รวมค่าข้อความจากหลายแถว
สร้างตารางและเพิ่มข้อมูล
mysql> create table DemoTable1611 ( Value text ); Query OK, 0 rows affected (0.86 sec)
mysql> insert into DemoTable1611 values('John');
Query OK, 1 row affected (0.11 sec)
mysql> insert into DemoTable1611 values('is');
Query OK, 1 row affected (0.17 sec)
mysql> insert into DemoTable1611 values('learning');
Query OK, 1 row affected (0.16 sec)
mysql> insert into DemoTable1611 values('Java');
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable1611 values('with');
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable1611 values('MySQL Database');
Query OK, 1 row affected (0.12 sec)
คิวรี่รวมข้อความด้วยช่องว่าง
mysql> select group_concat(Value separator ' ') from DemoTable1611;
ผลลัพธ์
+-------------------------------------------+ | group_concat(Value separator ' ') | +-------------------------------------------+ | John is learning Java with MySQL Database | +-------------------------------------------+ 1 row in set (0.00 sec)
ตัวเลือก: SEPARATOR กำหนดตัวคั่น (default คือ ',') | ใช้ ORDER BY ภายใน GROUP_CONCAT ได้ | มีขีดจำกัดความยาวค่าดีฟอลต์ 1024 ตัวอักษร (ปรับได้ด้วย group_concat_max_len)