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

标签:mysql,模糊查询,concat
0
投稿

猜你喜欢

  • PHP实现获取第一个中文首字母并进行排序的方法

    2023-10-30 12:29:08
  • Javascript调试之console对象——你不知道的一些小技巧

    2023-08-07 19:24:14
  • 巧制可全屏拖动的图片

    2008-05-09 19:34:00
  • Python程序控制语句用法实例分析

    2021-04-13 06:59:48
  • Python中标准库OS的常用方法总结大全

    2021-04-29 06:43:41
  • python复制列表时[:]和[::]之间有什么区别

    2021-08-15 01:22:05
  • pytorch中的weight-initilzation用法

    2022-03-22 23:28:24
  • 浅谈图像处理中掩膜(mask)的意义

    2021-03-17 01:48:50
  • tp5递归 无限级分类详解

    2024-05-11 09:53:48
  • 下载文件个别浏览器文件名乱码解决办法

    2024-04-17 10:05:04
  • Silverlight与Flash的技术比较

    2009-04-19 18:45:00
  • pyinstaller打包成无控制台程序时运行出错(与popen冲突的解决方法)

    2023-05-15 02:03:20
  • SQL Server应用程序的高级Sql注入

    2009-04-11 16:54:00
  • pytorch中fuse_modules源码解读

    2023-09-15 20:58:01
  • python画折线图的程序

    2021-07-04 18:08:20
  • keras自动编码器实现系列之卷积自动编码器操作

    2023-12-31 18:33:15
  • Python中的推导式使用详解

    2022-10-30 00:42:49
  • 用python绘制樱花树

    2023-07-24 19:47:09
  • 您是否记得关闭所有的XHTML元素

    2009-07-13 12:17:00
  • SQL Server连接中经常出现的3个常见错误与解答

    2010-07-26 14:25:00
  • asp之家 网络编程 m.aspxhome.com