前几篇文章给大家介绍了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拼接函数CONCAT的使用心得

不好意思,上面是我弟弟写的,正确的如下:

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,…)

"htmlcode">

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 ‘分隔符'] )

"text-align: center">详解MySQL拼接函数CONCAT的使用心得

又跑题了。。不好意思。

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)

"htmlcode">

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)

"htmlcode">

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拼接函数还是很好用的,希望你在有类似的需求或困惑时,想到它!或者能找到这篇博客,帮到你!

广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!

《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线

暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。

艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。

《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。