operating table
select * from test_concat order by id limit 5;
1.concat () function
Function: connecting a plurality of strings into a single string.
Syntax: the concat ( str1, str2, ...), returns the result to the connection parameters generated string, if there is any argument is null , then the return value is null .
3, for example:
select concat(area,fr,best_history_data) from test_concat order by id limit 5;
If you want to add a separator between fields, it is required to add a separator between each of the two fields , more trouble:
select concat(area,',',fr,',',best_history_data) as test_result from test_concat order by id limit 5;
2.concat_ws () function
Function: and concat (), as connecting a plurality of strings into a single string, but can be specified delimiter disposable ~ ( CONCAT_WS is concat with separator)
Syntax: CONCAT_WS (Separator , str1 , str2 , ... )
说明:第一个参数指定分隔符。需要注意的是分隔符不能为null,如果为null,则返回结果为null。
select concat_ws(',',area,fr,best_history_data) from test_concat order by id limit 5;
3.group_concat()函数
功能:将group by产生的同一个分组中的值连接起来,返回一个字符串结果。
语法:group_concat( [distinct] 要连接的字段 [order by 排序字段 asc/desc ] [separator '分隔符'] )
通过使用distinct可以排除重复值;如果希望对结果中的值进行排序,可以使用order by子句;separator是一个字符串值,缺省为一个逗号。
测试table:
select * from test_table;
1.根据area分组,拼接每个区域各个指标的指标值
select group_concat(fr,best_history_data) from test_table group by area;
2.增加分割符
select group_concat(fr,best_history_data separator '|') from test_table group by area;
3.结合concat_ws()函数,在fr与best_history_data之间增加分割符-
select group_concat(concat_ws('-',fr,best_history_data) separator '|') from test_table group by area;
4.根据best_history_data进行排序
select group_concat(concat_ws('-',fr,best_history_data) order by best_history_data desc separator '|') from test_table group by area;