mysql拼接多字段作为查询条件(Mysql 实现字段拼接的三个函数)
类别:数据库 浏览量:2736
时间:2022-03-29 03:38:36 mysql拼接多字段作为查询条件
Mysql 实现字段拼接的三个函数给运营导出数据时,难免需要对字段进行拼接,如果 Mysql 可以完成的话,就可以少些很多代码。
- concat()
- concat_ws()
- group_concat()
Mysql 确实有几个函数可以对字段进行拼接。
concat()
将多个字段使用空字符串拼接为一个字段
|
mysql> select concat(id, type) from mm_content limit 10; + ------------------+ | concat(id, type) | + ------------------+ | 100818image | | 100824image | | 100825video | | 100826video | | 100827video | | 100828video | | 100829video | | 100830video | | 100831video | | 100832video | + ------------------+ 10 rows in set (0.00 sec) |
不过如果有字段值为 NULL,则结果为 NULL。
|
mysql> select concat(id, type, tags) from mm_content limit 10; + ------------------------+ | concat(id, type, tags) | + ------------------------+ | NULL | | NULL | | NULL | | NULL | | NULL | | NULL | | NULL | | NULL | | NULL | | NULL | + ------------------------+ 10 rows in set (0.00 sec) |
concat_ws()
上面这种方式如果想要使用分隔符分割,就需要每个字段中间插一个字符串,非常麻烦。
concat_ws()
可以一次性的解决分隔符的问题,并且不会因为某个值为 NUll,而全部为 NUll。
|
mysql> select concat_ws( ' ' , id, type, tags) from mm_content limit 10; + --------------------------------+ | concat_ws( ' ' , id, type, tags) | + --------------------------------+ | 100818 image | | 100824 image | | 100825 video | | 100826 video | | 100827 video | | 100828 video | | 100829 video | | 100830 video | | 100831 video | | 100832 video | + --------------------------------+ 10 rows in set (0.00 sec) |
group_concat()
最后一个厉害了,正常情况下一个语句写成这样一定会报错的。
|
mysql> select id from test_user group by age; ERROR 1055 (42000): Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test_user.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by |
但是 group_concat()
可以将分组状态下的其他字段拼接成字符串查询出来
|
mysql> select group_concat( name ) from test_user group by age; + --------------------+ | group_concat( name ) | + --------------------+ | wen,ning | | wxnacy,win | + --------------------+ 2 rows in set (0.00 sec) |
默认使用逗号分隔,我们也可以指定分隔符
|
mysql> select group_concat( name separator ' ' ) from test_user group by age; + ----------------------------------+ | group_concat( name separator ' ' ) | + ----------------------------------+ | wen ning | | wxnacy win | + ----------------------------------+ 2 rows in set (0.00 sec) |
将字符串按照某个顺序排列
|
mysql> select group_concat( name order by id desc separator ' ' ) from test_user group by age; + ---------------------------------------------------+ | group_concat( name order by id desc separator ' ' ) | + ---------------------------------------------------+ | ning wen | | win wxnacy | + ---------------------------------------------------+ 2 rows in set (0.00 sec) |
如果想要拼接多个字段,默认是用空字符串进行拼接的,我们可以利用 concat_ws()
方法嵌套一层
|
mysql> select group_concat(concat_ws( ',' , id, name ) separator ' ' ) from test_user group by age; + ------------------------------------------------------+ | group_concat(concat_ws( ',' , id, name ) separator ' ' ) | + ------------------------------------------------------+ | 1,wen 2,ning | | 3,wxnacy 4,win | + ------------------------------------------------------+ 2 rows in set (0.00 sec) |
以上就是Mysql 实现字段拼接的三个函数的详细内容,更多关于MySQL 字符串拼接的资料请关注开心学习网其它相关文章!
原文链接:https://wxnacy.com/2020/07/30/mysql-concat/
您可能感兴趣
- docker运行redis并操作(Docker安装MySQL和Redis的方法步骤)
- mysql全表查询如何优化(MySQL 分组查询的优化方法)
- mysql拆分成多行(mysql单字段多值分割和合并的处理方法)
- 阿里云mysql配置升级注意事项(详解如何在阿里云上安装mysql)
- mysql索引知识点总结(MySQL 8.0 之索引跳跃扫描Index Skip Scan)
- mysqldecimal类型转换(mysql中decimal数据类型小数位填充问题详解)
- mysql主从复制忽略问题(MySQL复制问题的三个参数分析)
- mysql8.0.16安装步骤图解(mysql 8.0.22 安装配置图文教程)
- mysql字段多有什么问题(MySQL编码不一致可能引起的一些问题)
- mysql建立分区表指令(MySQL高级特性——数据表分区的概念及机制详解)
- mysql查看视图注释(详解MySQL的sql_mode查询与设置)
- mysql数据字段默认值怎么设置(MySQL 字段默认值该如何设置)
- mysql创建存储过程的代码(MySQL修改存储过程的详细步骤)
- MySql开启远程连接
- centos安装mysql8.0教程(Centos7 安装 Mysql8教程)
- druid数据库连接池原理详解(阿里巴巴Druid,轻松实现MySQL数据库加密!)
- 十二星座爱情支配欲指数(十二星座爱情支配欲指数)
- 虐待儿童是发泄支配欲的愚蠢行为(虐待儿童是发泄支配欲的愚蠢行为)
- 你或许不知道你隐藏的支配欲望(你或许不知道你隐藏的支配欲望)
- 把宽体丰田86卖了,换成7.5代高尔夫GTI玩起姿态与性能并存的改装(把宽体丰田86卖了)
- 大众推出了第五代高尔夫GT(大众推出了第五代高尔夫GT)
- 换代在即,现在是抄底 7.5代 高尔夫的最佳时机吗(换代在即现在是抄底)
热门推荐
- 宝塔怎么开放端口(宝塔面板修改默认端口以及一些常用端口的安全设置)
- pandas字符串和数字(使用pandas把某一列的字符值转换为数字的实例)
- SQL中Exists的用法
- SQL Server数据库备份的几种方式
- python中怎么实现队列的创建(python 堆和优先队列的使用详解)
- vue.js入门教学第15讲(Vue.js 使用AntV X6的示例步骤)
- phpstudy中php用什么版本(phpstudy是什么?phpstudy有什么用?)
- 云服务器建站要多大带宽(云服务器的带宽要多大?能容纳多少人?)
- python 提取微信消息(python实现文件助手中查看微信撤回消息)
- nodejs的广播机制(node.js如何自定义实现一个EventEmitter)