關于mysql函數concat與group_concat使用說明事項

1.concat()函數

使用方法:concat(str1,str2,…)
返回的結果為連接參數產生的字符串,如有任何一個參數為null,則返回值為null

注意:
如果所有參數均為非二進制字符串,則結果為非二進制字符串
如果自變量中含有任一二進制字符串,則結果為二進制字符串
一個數字參數被轉為與之相等的二進制字符串格式,如要避免這種情況,可使用顯式類型cast

例如:

select?concat(cast(int_col?as?char),?char_col);

使用例子:
1.字段兩端加上’,’

mysql>?select?concat(',',name,',')?from?`user`; +--------------------------+|?concat(',',fdipzone,',')?| +--------------------------+|?,fdipzone,???????????????| +--------------------------+1?row?in?set?(0.00?sec)

2.其中有一個參數為null

mysql>?select?concat(null,name)?from?`user`; +-------------------+|?concat(null,name)?| +-------------------+|?NULL??????????????| +-------------------+1?row?in?set?(0.00?sec)

2.concat_ws() 函數

使用方法:concat_ws(separator,str1,str2,…)

concat_ws()函數是concat()函數的特殊形式,第一個參數是其他參數的分隔符。分隔符的位置在要連接的兩個字符串之間。分隔符可以是一個字符串,也可以是其他參數。
如果分隔符為null,則結果為null。
函數會忽略任何分隔符參數后的null值,但concat_ws()不會忽略任何空字符串。

使用例子:
1.使用’,’分隔多個字段

mysql>?select?concat_ws(',',country_code,phone,region)?from?`user`; +------------------------------------------+|?concat_ws(',',country_code,phone,region)?| +------------------------------------------+|?86,13794830550,GZ????????????????????????| +------------------------------------------+1?row?in?set?(0.00?sec)

2.分隔符為null

mysql>?select?concat_ws(null,country_code,phone,region)?from?`user`; +-------------------------------------------+|?concat_ws(null,country_code,phone,region)?| +-------------------------------------------+|?NULL??????????????????????????????????????| +-------------------------------------------+1?row?in?set?(0.00?sec)

3.參數中有null與空字符串

mysql>?select?concat_ws(',',country_code,phone,null,region,'',grade)?from?`user`; +--------------------------------------------------------+|?concat_ws(',',country_code,phone,null,region,'',grade)?| +--------------------------------------------------------+|?86,13794830550,GZ,,200?????????????????????????????????| +--------------------------------------------------------+1?row?in?set?(0.00?sec)

3.group_concat()函數

使用方法:GROUP_CONCAT([DISTINCT] expr [,expr …]
[ORDER BY {unsigned_integer | col_name | formula} [ASC | DESC] [,col …]]
[SEPARATOR str_val])

group_concat可以得到表達式結合體的連結值,使用distinct可以排除重復值。使用order by子句可以排序。
separator是一個字符串,用于分隔結果集中每個元素。默認是逗號,可以通過指定separator “”完全移除這個分隔符。

使用例子:

表結構

CREATE?TABLE?`article_in_category`?(?`id`?int(11)?unsigned?NOT?NULL?AUTO_INCREMENT,?`article_id`?int(11)?unsigned?NOT?NULL,?`category_id`?int(11)?unsigned?NOT?NULL,?PRIMARY?KEY?(`id`),?KEY?`article_id_INDEX`?(`article_id`),?KEY?`category_id_INDEX`?(`category_id`) )?ENGINE=InnoDB?DEFAULT?CHARSET=utf8;

插入數據:

INSERT?INTO?`article_in_category`?(`id`,?`article_id`,?`category_id`)?VALUES?(NULL,?'1',?'1'), (NULL,?'1',?'2'),(NULL,?'1',?'3'),(NULL,?'2',?'4'),(NULL,?'2',?'3'),(NULL,?'2',?'5'),(NULL,?'3',?'1'), (NULL,?'3',?'5'),(NULL,?'3',?'6'),(NULL,?'4',?'8');
mysql>?select?*?from?`article_in_category`; +----+------------+-------------+|?id?|?article_id?|?category_id?| +----+------------+-------------+|??1?|??????????1?|???????????1?| |??2?|??????????1?|???????????2?| |??3?|??????????1?|???????????3?| |??4?|??????????2?|???????????4?| |??5?|??????????2?|???????????3?| |??6?|??????????2?|???????????5?| |??7?|??????????3?|???????????1?| |??8?|??????????3?|???????????5?| |??9?|??????????3?|???????????6?||?10?|??????????4?|???????????8?| +----+------------+-------------+

獲取文章的id及所有分類id

mysql>?select?article_id,group_concat(category_id?order?by?category_id?asc)?from?`article_in_category`?group?by?article_id; +------------+----------------------------------------------------+|?article_id?|?group_concat(category_id?order?by?category_id?asc)?| +------------+----------------------------------------------------+|??????????1?|?1,2,3??????????????????????????????????????????????| |??????????2?|?3,4,5??????????????????????????????????????????????| |??????????3?|?1,5,6??????????????????????????????????????????????||??????????4?|?8??????????????????????????????????????????????????| +------------+----------------------------------------------------+4?rows?in?set?(0.00?sec)

注意:group_concat()函數對返回的結果有長度限制,默認為1024字節

查看group_concat返回值最大長度

mysql>?show?global?variables?like?'%group_concat_max_len%'; +----------------------+-------+|?Variable_name????????|?Value?| +----------------------+-------+|?group_concat_max_len?|?1024??| +----------------------+-------+

修改group_concat返回值最大長度

mysql>?set?global?group_concat_max_len=2048; Query?OK,?0?rows?affected?(0.03?sec)mysql>?show?global?variables?like?'%group_concat_max_len%'; +----------------------+-------+|?Variable_name????????|?Value?| +----------------------+-------+|?group_concat_max_len?|?2048??| +----------------------+-------+

本文講解了mysql函數concat與group_concat使用說明,更多相關內容請關注php中文網。
相關推薦:

關于mysql innodb啟動失敗無法重啟的處理方法講解

關于mysql innodb啟動失敗無法重啟的處理方法講解

關于mysql innodb啟動失敗無法重啟的處理方法講解

以上就是關于

? 版權聲明
THE END
喜歡就支持一下吧
點贊6 分享