您的位置:首頁技術文章
文章詳情頁

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

瀏覽:116日期:2023-02-18 16:43:35
目錄
  • 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()內容請搜索以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持!

標簽: MySQL
国产综合久久一区二区三区