欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

mysql 模糊查詢 concat()的用法詳解

 更新時(shí)間:2023年02月14日 09:27:13   作者:weixin_30872157  
大家都知道concat()函數(shù),是用來(lái)連接字符串,今天通過本文給大家介紹mysql 模糊查詢 concat()及concat的用法,感興趣的朋友跟隨小編一起看看吧

mysql 模糊查詢 concat()

concat() 函數(shù),是用來(lái)連接字符串。

精確查詢: select * from user where name=”zhangsan” 
模糊查詢; select * from user where name like “%zhang%”

在實(shí)際的使用中,條件是作為參數(shù)傳遞進(jìn)來(lái)的。 所以我們使用 concat() 函數(shù)

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,……….); 連接字符串函數(shù),會(huì)生成一個(gè)字符串 

補(bǔ)充:MySQL之concat的用法

一、concat()函數(shù)

1、功能:將多個(gè)字符串連接成一個(gè)字符串。

2、語(yǔ)法:concat(str1, str2,...)

說明:返回結(jié)果為連接參數(shù)產(chǎn)生的字符串,如果有任何一個(gè)參數(shù)為null,則返回值為null。

3、舉例:select concat (id, name, score) as 別名 from 表名;

二、concat_ws()函數(shù)

1、功能:和concat()一樣,但是可以指定分隔符(concat_ws就是concat with separator)

2、語(yǔ)法:concat_ws(separator, str1, str2, ...)

說明:第一個(gè)參數(shù)指定分隔符。需要注意的是分隔符不能為null,如果為null,則返回結(jié)果為null。

3、舉例:select concat ('#',id, name, score) as 別名 from 表名;

三、group_concat()函數(shù)

1、功能:將group by產(chǎn)生的同一個(gè)分組中的值連接起來(lái),返回一個(gè)字符串結(jié)果。

2、語(yǔ)法:group_concat( [distinct] 要連接的字段 [order by 排序字段 asc/desc ] [separator] )

說明:通過使用distinct可以排除重復(fù)值;如果希望對(duì)結(jié)果中的值進(jìn)行排序,可以使用order by子句;separator分隔符是一個(gè)字符串值,缺省為一個(gè)逗號(hào)。

3、舉例:select name,group_concat(id order by id desc separator '#') as 別名 from 表名 group by name;

四、concat_ws()和group_concat()聯(lián)合使用

題目:查詢以name分組的所有組的id和score

舉例:select name,group_concat(concat_ws('-',id,score) order by id) as 別名 from 表名 group by name;

到此這篇關(guān)于mysql 模糊查詢 concat()的文章就介紹到這了,更多相關(guān)mysql 模糊查詢 concat()內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論