详解MySQL拼接函数CONCAT的使用心得
前几篇文章给大家介绍了mysql中的替换函数(replace)、切分函数(substring),今天我们一起来看看mysql专业拼接“字符串”的函数:concat。老规矩,有好的建议和想法,记得写到评论中,等我上班摸鱼时,跟大家一起吃瓜~
一、concat函数相关的几种用法
1-1、函数:concat(str1,str2,…)
concat 函数一般用在select 查询语法中,用于修改返回字段内容,例如有张lol英雄信息表如下
mysql> select * from `lol`; +----+---------------+--------------+-------+ | id | hero_title | hero_name | price | +----+---------------+--------------+-------+ | 1 | d刀锋之影 | 泰隆 | 6300 | | 2 | x迅捷斥候 | 提莫 | 6300 | | 3 | g光辉女郎 | 拉克丝 | 1350 | | 4 | f发条魔灵 | 奥莉安娜 | 6300 | | 5 | z至高之拳 | 李青 | 6300 | | 6 | w无极剑圣 | 易 | 450 | | 7 | j疾风剑豪 | 亚索 | 450 | +----+---------------+--------------+-------+ 7 rows in set (0.00 sec)
我需要返回一列:英雄称号 - 英雄名称 的数据,这是就用到了concat函数,如下:
select concat(hero_title,' - ',hero_name) as full_name, price from `lol`;
mysql> select concat(hero_title,' - ',hero_name) as full_name, price from `lol`; +------------------------------+-------+ | full_name | price | +------------------------------+-------+ | d刀锋之影 - 泰隆 | 6300 | | x迅捷斥候 - 提莫 | 6300 | | g光辉女郎 - 拉克丝 | 1350 | | f发条魔灵 - 奥莉安娜 | 6300 | | z至高之拳 - 李青 | 6300 | | w无极剑圣 - 易 | 450 | | j疾风剑豪 - 亚索 | 450 | +------------------------------+-------+ 7 rows in set (0.00 sec)
如果拼接的参数中有null,则返回null;如下:
select concat(hero_title,null,hero_name) as full_name, price from `lol`;
mysql> select concat(hero_title,'null',hero_name) as full_name, price from `lol`; +-------------------------------+-------+ | full_name | price | +-------------------------------+-------+ | d刀锋之影null泰隆 | 6300 | | x迅捷斥候null提莫 | 6300 | | g光辉女郎null拉克丝 | 1350 | | f发条魔灵null奥莉安娜 | 6300 | | z至高之拳null李青 | 6300 | | w无极剑圣null易 | 450 | | j疾风剑豪null亚索 | 450 | +-------------------------------+-------+ 7 rows in set (0.00 sec)
不好意思,上面是我弟弟写的,正确的如下:
mysql> select concat(hero_title,null,hero_name) as full_name, price from `lol`; +-----------+-------+ | full_name | price | +-----------+-------+ | null | 6300 | | null | 6300 | | null | 1350 | | null | 6300 | | null | 6300 | | null | 450 | | null | 450 | +-----------+-------+ 7 rows in set (0.00 sec)
1-2、函数:concat_ws(separator,str1,str2,…)
concat_ws() 函数全称: concat with separator ,是concat()的特殊形式。第一个参数(separator)是其它参数的分隔符。分隔符的位置在要连接的两个字符串之间。分隔符可以是一个字符串,也可以是其它字段参数。
需要注意的是:
如果分隔符为 null,则结果为 null;但如果分隔符后面的参数为null,只会被直接忽略掉,而不会导致结果为null。
好了,我们依旧用上面的lol表,连接各字段,以逗号分隔:
select concat_ws(',',hero_title,hero_name,price) as full_name, price from `lol`;
mysql> select concat_ws(',',hero_title,hero_name,price) as full_name, price from `lol`; +---------------------------------+-------+ | full_name | price | +---------------------------------+-------+ | d刀锋之影,泰隆,6300 | 6300 | | x迅捷斥候,提莫,6300 | 6300 | | g光辉女郎,拉克丝,1350 | 1350 | | f发条魔灵,奥莉安娜,6300 | 6300 | | z至高之拳,李青,6300 | 6300 | | w无极剑圣,易,450 | 450 | | j疾风剑豪,亚索,450 | 450 | +---------------------------------+-------+ 7 rows in set (0.00 sec)
分隔符后的拼接参数为null时,直接忽略,不会影响整体结果,如下:
select concat_ws(',',hero_title,null,hero_name) as full_name, price from `lol`;
mysql> select concat_ws(',',hero_title,null,hero_name) as full_name, price from `lol`; +----------------------------+-------+ | full_name | price | +----------------------------+-------+ | d刀锋之影,泰隆 | 6300 | | x迅捷斥候,提莫 | 6300 | | g光辉女郎,拉克丝 | 1350 | | f发条魔灵,奥莉安娜 | 6300 | | z至高之拳,李青 | 6300 | | w无极剑圣,易 | 450 | | j疾风剑豪,亚索 | 450 | +----------------------------+-------+ 7 rows in set (0.00 sec)
分隔符为null时,结果返回null,如下:
select concat_ws(null,hero_title,hero_name,price) as full_name, price from `lol`;
mysql> select concat_ws(null,hero_title,hero_name,price) as full_name, price from `lol`; +-----------+-------+ | full_name | price | +-----------+-------+ | null | 6300 | | null | 6300 | | null | 1350 | | null | 6300 | | null | 6300 | | null | 450 | | null | 450 | +-----------+-------+ 7 rows in set (0.00 sec)
1-3、函数:group_concat(expr)
group_concat ( [distinct] 字段名 [order by 排序字段 asc/desc] [separator ‘分隔符'] )
group_concat函数通常用于有group by的查询语句,group_concat一般包含在查询返回结果字段中。
是不是group_concat函数的公式看着还挺复杂的?我们一起看看,上方公式中 [] 括号是可选项,表示可用可不用;
- 1.
[distinct]
:对拼接的参数支持去重功能; - 2.
[order by]
:拼接的参数支持排序功能; - 3.
[separator]
:这个你很熟悉了,支持自定义'分隔符',如不设置默认为无分隔符;
好了,下面让我们来进入开心的测试环节吧~ 还是用这张lol表,别问为什么。或许这就是青春吧!还记的那年通宵五排,大龙团的时候网吧停电了…
又跑题了。。不好意思。
mysql> select * from `lol`; +----+---------------+--------------+-------+ | id | hero_title | hero_name | price | +----+---------------+--------------+-------+ | 1 | d刀锋之影 | 泰隆 | 6300 | | 2 | x迅捷斥候 | 提莫 | 6300 | | 3 | g光辉女郎 | 拉克丝 | 1350 | | 4 | f发条魔灵 | 奥莉安娜 | 6300 | | 5 | z至高之拳 | 李青 | 6300 | | 6 | w无极剑圣 | 易 | 450 | | 7 | j疾风剑豪 | 亚索 | 450 | +----+---------------+--------------+-------+ 7 rows in set (0.00 sec)
举个场景,我们要区分出各个价格段的英雄来,如果不用group_concat的话,或许只能用order by区分了。
select * from `lol` order by price desc;
mysql> select * from `lol` order by price desc; +----+---------------+--------------+-------+ | id | hero_title | hero_name | price | +----+---------------+--------------+-------+ | 1 | d刀锋之影 | 泰隆 | 6300 | | 2 | x迅捷斥候 | 提莫 | 6300 | | 4 | f发条魔灵 | 奥莉安娜 | 6300 | | 5 | z至高之拳 | 李青 | 6300 | | 3 | g光辉女郎 | 拉克丝 | 1350 | | 6 | w无极剑圣 | 易 | 450 | | 7 | j疾风剑豪 | 亚索 | 450 | +----+---------------+--------------+-------+ 7 rows in set (0.00 sec)
但是这样很不直观啊,我想一行都看到,怎么办?
select group_concat(hero_title,' - ',hero_name separator ',' ) as full_name, price from `lol` group by price order by price desc;
这时group_concat函数就轻松的帮你解决了这个问题。看,舒服不~
注释:这里我是拼接了(hero_title,' - ‘,hero_name)这三个参数,分隔符设为:',',根据价格来分组,根据价格来排的序。效果如下
mysql> select group_concat(hero_title,' - ',hero_name separator ',' ) as full_name, price from `lol` group by price order by price desc; +------------------------------------------------------------------------+-------+ | full_name | price | +------------------------------------------------------------------------+-------+ | d刀锋之影 - 泰隆,x迅捷斥候 - 提莫,f发条魔灵 - 奥莉安娜,z至高之拳 - 李青 | 6300 | | g光辉女郎 - 拉克丝 | 1350 | | w无极剑圣 - 易,j疾风剑豪 - 亚索 | 450 | +------------------------------------------------------------------------+-------+ 3 rows in set (0.00 sec)
如果按价格(price)从小到大排序,只需控制外层order by即可,如下:
select group_concat(hero_title,' - ',hero_name separator ',' ) as full_name, price from `lol` group by price order by price asc;
mysql> select group_concat(hero_title,' - ',hero_name separator ',' ) as full_name, price from `lol` group by price order by price asc; +-------------------------------------------------------------------------+-------+ | full_name | price | +-------------------------------------------------------------------------+-------+ | w无极剑圣 - 易,j疾风剑豪 - 亚索 | 450 | | g光辉女郎 - 拉克丝 | 1350 | | d刀锋之影 - 泰隆,x迅捷斥候 - 提莫,f发条魔灵 - 奥莉安娜,z至高之拳 - 李青 | 6300 | +-------------------------------------------------------------------------+-------+ 3 rows in set (0.00 sec)
那么group_concat函数中的order by 排序怎么用?是用在了拼接字段的排序上,如根据hero_title进行排序拼接,如下:
select group_concat(hero_title,' - ',hero_name order by hero_title separator ',' ) as full_name, price from `lol` group by price order by price asc;
mysql> select group_concat(hero_title,' - ',hero_name order by hero_title separator ',' ) as full_name, price from `lol` group by price order by price asc; +-------------------------------------------------------------------------+-------+ | full_name | price | +-------------------------------------------------------------------------+-------+ | j疾风剑豪 - 亚索,w无极剑圣 - 易 | 450 | | g光辉女郎 - 拉克丝 | 1350 | | d刀锋之影 - 泰隆,f发条魔灵 - 奥莉安娜,x迅捷斥候 - 提莫,z至高之拳 - 李青 | 6300 | +-------------------------------------------------------------------------+-------+ 3 rows in set (0.00 sec)
好了,可以看出,mysql中的concat拼接函数还是很好用的,希望你在有类似的需求或困惑时,想到它!或者能找到这篇博客,帮到你!
到此这篇关于详解mysql拼接函数concat的使用心得的文章就介绍到这了,更多相关mysql拼接函数concat内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
上一篇: 从判断闰年开始吧!
下一篇: 房事养生也该随“秋”而变