使用group_concat函數來實現功能
This function returns a string result with the concatenated non-NULL values from a group. It returns NULL if there are no non-NULL values.
--Mysql官方文檔
mysql> SELECT student_name,
GROUP_CONCAT(test_score)
FROM student
GROUP BY student_name;
一定要加上group by條件,否則不規范
用戶可能會碰上一種情況,就是拼接的字符串太長,結果顯示不全,這種情況需要使用如下指令:
SET SESSION group_concat_max_len = 10240;
在這里設置Session后,會作用在當前會話上,直到用戶關閉客戶端,重新連接mysql數據庫為止。