mysql concat 、concat_ws group_concat函數用法

1、concat()函數能夠鏈接一個或者多個字符串ide

  CONCAT(str1,str2,…) 返回結果爲鏈接參數產生的字符串。若有任何一個參數爲NULL ,則返回值爲 NULL。函數

  select concat('11','22','33');     112233排序

2、CONCAT_WS(separator,str1,str2,...) 字符串

  是CONCAT()的特殊形式。第一個參數是其它參數的分隔符。分隔符的位置放在要鏈接的兩個字符串之間。分隔符能夠是一個字符串,也能夠是其它參數。it

    select concat_ws(',','11','22','33');    11,22,33class

3、group_concat()分組拼接函數select

  group_concat([DISTINCT] 要鏈接的字段 [Order BY ASC/DESC 排序字段] [Separator '分隔符'])數據

  對下面的一組數據使用 group_concat()di

  | id |nameview

  |1 | 10|
  |1 | 20|
  |1 | 20|
  |2 | 20|
  |3 | 200   |
  |3 | 500   |

  一、select id,group_concat(name) from aa group by id;

  |1 | 10,20,20|
  |2 | 20 |
  |3 | 200,500|

  二、select id,group_concat(name separator ';') from aa group by id;

  |1 | 10;20;20 |
  |2 | 20|
  |3 | 200;500   |

  三、select id,group_concat(name order by name desc) from aa group by id;

  |1 | 20,20,10   |
  |2 | 20|
  |3 | 500,200|

  四、select id,group_concat(distinct name) from aa group by id;

  |1 | 10,20|  |2 | 20   |  |3 | 200,500 |

相關文章
相關標籤/搜索