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运用说明,更多相干内容请关注ki4网。
相干引荐:
关于mysql innodb启动失利没法重启的处置惩罚要领解说
解说php猎取指定日期的相干内容
详解PHP生成唯一RequestID类
以上就是关于mysql函数concat与group_concat运用说明事项的细致内容,更多请关注ki4网别的相干文章!