標籤:函數 cat tor code -- bsp style des asc
函數文法:group_concat( [DISTINCT] 要串連的欄位 [Order BY 排序欄位 ASC/DESC] [Separator ‘分隔字元‘] )
下面舉例說明:select * from goods; +------+------+| id| price|+------+------+|1 | 10||1 | 20||1 | 20||2 | 20||3 | 200 ||3 | 500 |+------+------+6 rows in set (0.00 sec)以id分組,把price欄位的值在同一行列印出來,逗號分隔(預設)select id, group_concat(price) from goods group by id; +------+--------------------+| id| group_concat(price) |+------+--------------------+|1 | 10,20,20||2 | 20 ||3 | 200,500|+------+--------------------+3 rows in set (0.00 sec)以id分組,把price欄位的值在一行列印出來,分號分隔 select id,group_concat(price separator ‘;‘) from goods group by id; +------+----------------------------------+| id| group_concat(price separator ‘;‘) |+------+----------------------------------+|1 | 10;20;20 ||2 | 20||3 | 200;500 |+------+----------------------------------+3 rows in set (0.00 sec)以id分組,把去除重複冗餘的price欄位的值列印在一行,逗號分隔select id,group_concat(distinct price) from goods group by id; +------+-----------------------------+| id| group_concat(distinct price) |+------+-----------------------------+|1 | 10,20||2 | 20 ||3 | 200,500 |+------+-----------------------------+3 rows in set (0.00 sec)以id分組,把price欄位的值列印在一行,逗號分隔,按照price倒序排列select id,group_concat(price order by price desc) from goods group by id; +------+---------------------------------------+| id| group_concat(price order by price desc) |+------+---------------------------------------+|1 | 20,20,10 ||2 | 20||3 | 500,200|+------+---------------------------------------+3 rows in set (0.00 sec)
mysql group_concat函數