文章詳情頁
SQL實現(xiàn)模糊查詢的四種方法總結(jié)
瀏覽:8日期:2023-09-25 20:57:30
目錄一、一般模糊查詢二、利用通配符查詢1. _ 表示任意的單個字符2. % 表示匹配任意多個任意字符3. [ ]表示篩選范圍4. 查詢包含通配符的字符串
模糊查詢是針對字符串操作的,類似正則表達式,沒有正則表達式強大。
一、一般模糊查詢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. _ 表示任意的單個字符//查詢所有名字姓張,字長兩個字的記錄select * from student where name like '張_'//查詢所有名字姓張,字長三個字的記錄select * from student where name like '張__'2. % 表示匹配任意多個任意字符//查詢所有名字姓張,字長不限的記錄select * from student where name like '張%'//查詢所有名字姓張,字長兩個字的記錄select * from student where name like '張%'and len(name) = 23. [ ]表示篩選范圍//查詢所有名字姓張,第二個為數(shù)字,第三個為燕的記錄select * from student where name like '張[0-9]燕'//查詢所有名字姓張,第二個為字母,第三個為燕的記錄select * from student where name like '張[a-z]燕'//查詢所有名字姓張,中間為1個字母或1個數(shù)字,第三個為燕的名字。字母大小寫可以通過約束設(shè)定,不區(qū)分大小寫select * from student where name like '張[0-9a-z]燕'//查詢所有名字姓張,第二個不為數(shù)字,第三個為燕的記錄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實現(xiàn)模糊查詢的四種方法總結(jié)的文章就介紹到這了,更多相關(guān)SQL 模糊查詢內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
標(biāo)簽:
MsSQL
數(shù)據(jù)庫
相關(guān)文章:
1. DB2比較常用與實用sql語句總結(jié)2. db2v8的pdf文檔資料3. MySQL分支選擇參考:Percona還是MariaDB4. short int、long、float、double使用問題說明5. MySQL數(shù)據(jù)庫基礎(chǔ)學(xué)習(xí)之JSON函數(shù)各類操作詳解6. 恢復(fù)從 Access 2000、 Access 2002 或 Access 2003 中數(shù)據(jù)庫刪除表的方法7. db2 導(dǎo)入導(dǎo)出單個表的操作詳解8. MySQL導(dǎo)入sql文件的三種方法小結(jié)9. 關(guān)于MariaDB安裝問題小記(CMake Error at)10. Oracle中分割字符串的方法實例代碼
排行榜
