sql語句實現模糊搜尋

來源:互聯網
上載者:User

SQL模糊查詢,使用like比較字,加上SQL裡的萬用字元,請參考以下:
1、LIKE'Mc%' 將搜尋以字母 Mc 開頭的所有字串(如 McBadden)。
2、LIKE'%inger' 將搜尋以字母 inger 結尾的所有字串(如 Ringer、Stringer)。
3、LIKE'%en%' 將搜尋在任何位置包含字母 en 的所有字串(如 Bennet、Green、McBadden)。
4、LIKE'_heryl' 將搜尋以字母 heryl 結尾的所有六個字母的名稱(如 Cheryl、Sheryl)。
5、LIKE'[CK]ars[eo]n' 將搜尋下列字串:Carsen、Karsen、Carson 和 Karson(如 Carson)。
6、LIKE'[M-Z]inger' 將搜尋以字串 inger 結尾、以從 M 到 Z 的任何單個字母開頭的所有名稱(如 Ringer)。
7、LIKE'M[^c]%' 將搜尋以字母 M 開頭,並且第二個字母不是 c 的所有名稱(如MacFeather)。
-------------------------------------------------
呵呵,要完整的例句啊。下面這句查詢字串是我以前寫的,根據變數 zipcode_key 在郵遞區號表 zipcode 中查詢對應的資料,這句是判斷變數 zipcode_key 為非數字時的查詢語句,用 % 來匹配任意長度的字串,從表中地址、市、省三列中查詢包含關鍵字的所有資料項目,並按省、市、地址排序。這個例子比較簡單,只要你理解了方法就可以寫出更複雜的查詢語句。

sql = "select * from zipcode where (address like'%" & zipcode_key & "%') or (city like'%" & zipcode_key & "%') or (province like'%" & zipcode_key & "%') order by province,city,address"

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.