mysql 模糊查詢 concat()的用法詳解
mysql 模糊查詢 concat()
concat() 函數(shù),是用來連接字符串。
精確查詢: select * from user where name=”zhangsan”
模糊查詢; select * from user where name like “%zhang%”
在實際的使用中,條件是作為參數(shù)傳遞進來的。 所以我們使用 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ù),會生成一個字符串
補充:MySQL之concat的用法
一、concat()函數(shù)
1、功能:將多個字符串連接成一個字符串。
2、語法:concat(str1, str2,...)
說明:返回結(jié)果為連接參數(shù)產(chǎn)生的字符串,如果有任何一個參數(shù)為null,則返回值為null。
3、舉例:select concat (id, name, score) as 別名 from 表名;
二、concat_ws()函數(shù)
1、功能:和concat()一樣,但是可以指定分隔符(concat_ws就是concat with separator)
2、語法:concat_ws(separator, str1, str2, ...)
說明:第一個參數(shù)指定分隔符。需要注意的是分隔符不能為null,如果為null,則返回結(jié)果為null。
3、舉例:select concat ('#',id, name, score) as 別名 from 表名;
三、group_concat()函數(shù)
1、功能:將group by產(chǎn)生的同一個分組中的值連接起來,返回一個字符串結(jié)果。
2、語法:group_concat( [distinct] 要連接的字段 [order by 排序字段 asc/desc ] [separator] )
說明:通過使用distinct可以排除重復(fù)值;如果希望對結(jié)果中的值進行排序,可以使用order by子句;separator分隔符是一個字符串值,缺省為一個逗號。
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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- MySQL基于group_concat()函數(shù)合并多行數(shù)據(jù)
- MySQL中組合字段之concat()
- Mybatis?mysql模糊查詢方式(CONCAT多個字段)及bug
- MySQL中CONCAT和GROUP_CONCAT方法的區(qū)別詳解
- Mysql中的concat函數(shù)(拼接函數(shù))詳解
- MySQL group_concat函數(shù)使用方法詳解
- mysql中的concat()函數(shù)模糊查詢代替${}問題
- mysql中GROUP_CONCAT函數(shù)使用及遇到問題詳解
- mysql中GROUP_CONCAT函數(shù)使用技巧及問題詳解
- MySQL中的CONCAT()函數(shù):輕松拼接字符串的利器
- MySQL中CONCAT()函數(shù)出現(xiàn)值為空的問題及解決辦法
相關(guān)文章
mysql創(chuàng)建外鍵報錯的原因及解決(can't?not?create?table)
這篇文章主要介紹了mysql創(chuàng)建外鍵報錯的原因及解決方案(can't?not?create?table),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-09-09ERROR 1406 : Data too long for column 解決辦法
導(dǎo)入數(shù)據(jù)的時候,mysql報錯 ERROR 1406 : Data too long for column Data too long for column2011-04-04MySQL索引下推index condition pushdown
索引下推是MySQL 5.6版本引入的一種數(shù)據(jù)庫查詢優(yōu)化技術(shù),本文主要介紹了MySQL索引下推index condition pushdown,具有一定的參考價值,感興趣的可以了解一下2024-08-08如何在Java程序中訪問mysql數(shù)據(jù)庫中的數(shù)據(jù)并進行簡單的操作
這篇文章主要介紹了如何在Java程序中訪問mysql數(shù)據(jù)庫中的數(shù)據(jù)并進行簡單的操作的相關(guān)資料,需要的朋友可以參考下2016-05-05