文章詳情頁
SQL實(shí)現(xiàn)模糊查詢的四種方法總結(jié)
瀏覽:3日期:2023-09-25 20:57:30
目錄一、一般模糊查詢二、利用通配符查詢1. _ 表示任意的單個(gè)字符2. % 表示匹配任意多個(gè)任意字符3. [ ]表示篩選范圍4. 查詢包含通配符的字符串
模糊查詢是針對字符串操作的,類似正則表達(dá)式,沒有正則表達(dá)式強(qiáng)大。
一、一般模糊查詢1. 單條件查詢
//查詢所有姓名包含“張”的記錄select * from student where name like '張'2. 多條件查詢
//查詢所有姓名包含“張”,地址包含四川的記錄select * from student where name like '張' and address like '四川'//查詢所有姓名包含“張”,或者地址包含四川的記錄select * from student where name like '張' or address like '四川'二、利用通配符查詢通配符:_ 、% 、[ ]
1. _ 表示任意的單個(gè)字符//查詢所有名字姓張,字長兩個(gè)字的記錄select * from student where name like '張_'//查詢所有名字姓張,字長三個(gè)字的記錄select * from student where name like '張__'2. % 表示匹配任意多個(gè)任意字符//查詢所有名字姓張,字長不限的記錄select * from student where name like '張%'//查詢所有名字姓張,字長兩個(gè)字的記錄select * from student where name like '張%'and len(name) = 23. [ ]表示篩選范圍//查詢所有名字姓張,第二個(gè)為數(shù)字,第三個(gè)為燕的記錄select * from student where name like '張[0-9]燕'//查詢所有名字姓張,第二個(gè)為字母,第三個(gè)為燕的記錄select * from student where name like '張[a-z]燕'//查詢所有名字姓張,中間為1個(gè)字母或1個(gè)數(shù)字,第三個(gè)為燕的名字。字母大小寫可以通過約束設(shè)定,不區(qū)分大小寫select * from student where name like '張[0-9a-z]燕'//查詢所有名字姓張,第二個(gè)不為數(shù)字,第三個(gè)為燕的記錄select * from student where name like '張[!0-9]燕'?//查詢名字除了張開頭妹結(jié)尾中間是數(shù)字的記錄select * from student where name not like '張[0-9]燕'4. 查詢包含通配符的字符串//查詢姓名包含通配符%的記錄?select * from student where name like '%[%]%'?? ??? ??? ??? ?//通過[]轉(zhuǎn)義//查詢姓名包含[的記錄?select * from student where name like '%/[%' escape '/'?? ?//通過指定'/'轉(zhuǎn)義//查詢姓名包含通配符[]的記錄?select * from student where name like '%/[/]%' escape '/'?? ?//通過指定'/'轉(zhuǎn)義到此這篇關(guān)于SQL實(shí)現(xiàn)模糊查詢的四種方法總結(jié)的文章就介紹到這了,更多相關(guān)SQL 模糊查詢內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
標(biāo)簽:
MsSQL
數(shù)據(jù)庫
相關(guān)文章:
1. sql server添加唯一性約束的方法步驟2. 有時(shí)Oracle不用索引來查找數(shù)據(jù)的原因3. MySQL之高可用集群部署及故障切換實(shí)現(xiàn)4. MariaDB的安裝與配置教程5. Centos7 下mysql重新啟動MariaDB篇6. 利用sql server 2005數(shù)據(jù)庫郵件發(fā)送電子郵件7. MySQL 編碼utf8 與 utf8mb4 utf8mb4_unicode_ci 與 utf8mb4_general_ci8. 詳解mysql的備份與恢復(fù)9. sql server 2005中的表分區(qū)10. 使用DB2look 重新創(chuàng)建優(yōu)化器訪問計(jì)劃(3)(1)
排行榜
