mysql 模糊查询 concat()的用法详解
作者:weixin_30872157 时间:2024-01-14 01:48:46
mysql 模糊查询 concat()
concat() 函数,是用来连接字符串。
精确查询: select * from user where name=”zhangsan”
模糊查询; select * from user where name like “%zhang%”
在实际的使用中,条件是作为参数传递进来的。 所以我们使用 concat() 函数
mybatis:
select * from user where name like concat(“%”, #{name},”%”)
原生SQL:
case when ?1 is null then 1=1 else name like CONCAT('%',?1,'%')
END
concat(str1,str2,str3,str4,……….); 连接字符串函数,会生成一个字符串
补充:MySQL之concat的用法
一、concat()函数
1、功能:将多个字符串连接成一个字符串。
2、语法:concat(str1, str2,...)
说明:返回结果为连接参数产生的字符串,如果有任何一个参数为null,则返回值为null。
3、举例:select concat (id, name, score) as 别名 from 表名;
二、concat_ws()函数
1、功能:和concat()一样,但是可以指定分隔符(concat_ws就是concat with separator)
2、语法:concat_ws(separator, str1, str2, ...)
说明:第一个参数指定分隔符。需要注意的是分隔符不能为null,如果为null,则返回结果为null。
3、举例:select concat ('#',id, name, score) as 别名 from 表名;
三、group_concat()函数
1、功能:将group by产生的同一个分组中的值连接起来,返回一个字符串结果。
2、语法:group_concat( [distinct] 要连接的字段 [order by 排序字段 asc/desc ] [separator] )
说明:通过使用distinct可以排除重复值;如果希望对结果中的值进行排序,可以使用order by子句;separator分隔符是一个字符串值,缺省为一个逗号。
3、举例:select name,group_concat(id order by id desc separator '#') as 别名 from 表名 group by name;
四、concat_ws()和group_concat()联合使用
题目:查询以name分组的所有组的id和score
举例:select name,group_concat(concat_ws('-',id,score) order by id) as 别名 from 表名 group by name;
来源:https://blog.csdn.net/weixin_30872157/article/details/96705650
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
PHP实现获取第一个中文首字母并进行排序的方法
Javascript调试之console对象——你不知道的一些小技巧
![](https://img.aspxhome.com/file/2023/4/55904_0s.jpg)
巧制可全屏拖动的图片
![](https://img.aspxhome.com/file/UploadPic/20072/20072311372391s.jpg)
Python程序控制语句用法实例分析
![](https://img.aspxhome.com/file/2023/6/72146_0s.png)
Python中标准库OS的常用方法总结大全
python复制列表时[:]和[::]之间有什么区别
pytorch中的weight-initilzation用法
浅谈图像处理中掩膜(mask)的意义
tp5递归 无限级分类详解
下载文件个别浏览器文件名乱码解决办法
Silverlight与Flash的技术比较
pyinstaller打包成无控制台程序时运行出错(与popen冲突的解决方法)
![](https://img.aspxhome.com/file/2023/5/78485_0s.png)
SQL Server应用程序的高级Sql注入
pytorch中fuse_modules源码解读
![](https://img.aspxhome.com/file/2023/7/97347_0s.png)
python画折线图的程序
![](https://img.aspxhome.com/file/2023/5/131115_0s.jpg)
keras自动编码器实现系列之卷积自动编码器操作
Python中的推导式使用详解
用python绘制樱花树
![](https://img.aspxhome.com/file/2023/2/128212_0s.png)