您当前的位置: 首页-5G-详情

mysql 模糊查询 concat()的用法详解_焦点报道

2023-06-16 14:59:20来源:脚本之家
目录
mysql 模糊查询 concat()补充:MySQL之concat的用法一、concat()函数二、concat_ws()函数三、group_concat()函数四、concat_ws()和group_concat()联合使用

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;

到此这篇关于mysql 模糊查询 concat()的文章就介绍到这了,更多相关mysql 模糊查询 concat()内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

标签:

上一篇:Smart两厢车怎么样?城市驾驶首选的小型车型!
下一篇:最后一页