sql模糊查詢語句詳解

來源:互聯網
上載者:User

在sql中要實現模糊查詢我們只要用到like就可以實現了,中間再帶一些參數如% ? 等,下面來看個執行個體。
LIKE 操作符
LIKE 操作符用於在 WHERE 子句中搜尋列中的指定模式。

SQL LIKE 操作符文法

SELECT column_name(s)FROM table_nameWHERE column_name LIKE

1> CREATE TABLE employee  (emp_no    INTEGER NOT NULL,
2>                         emp_fname CHAR(20) NOT NULL,
3>                         emp_lname CHAR(20) NOT NULL,
4>                         dept_no   CHAR(4) NULL)
5> GO
1> insert into employee values(1,  'Matthew', 'Smith',    'd3')
2> insert into employee values(2,  'Ann',     'Jones',    'd3')
3> insert into employee values(3,  'John',    'Barrimore','d1')
4> insert into employee values(4,  'James',   'James',    'd2')
5> insert into employee values(5,  'Elsa',    'Bertoni',  'd2')
6> insert into employee values(6,  'Elke',    'Hansel',   'd2')
7> insert into employee values(7,  'Sybill',  'Moser',    'd1')
8> GO


> SELECT * FROM employee WHERE emp_fname NOT LIKE '%n'
4> GO
emp_no      emp_fname            emp_lname            dept_no
----------- -------------------- -------------------- -------
          1 Matthew              Smith                d3
          4 James                James                d2
          5 Elsa                 Bertoni              d2
          6 Elke                 Hansel               d2
          7 Sybill               Moser                d1

 

執行個體二

2> SELECT *
3> FROM Employee
4> WHERE Name LIKE "%[k-l]%"
5> GO
ID          name       salary      start_date              city       region
----------- ---------- ----------- ----------------------- ---------- ------
          3 Celia            24020 1996-12-03 00:00:00.000 Toronto    W
          4 Linda            40620 1997-11-04 00:00:00.000 New York   N
          7 Alison           90620 2000-08-07 00:00:00.000 New York   W

 

關於like %%

# %代表任意多個字元

select * from user where username like '%huxiao';

select * from user where username like 'huxiao%';

select * from user where username like '%huxiao%';

# %代表一個字元

select * from user where username like '_';

select * from user where username like '______';

select * from user where username like 'huxia_';

select * from user where username like 'h_xiao';


# 如果我就真的要查%或者_,怎麼辦呢?使用escape,逸出字元後面的%或_就不作為萬用字元了,注意前面沒有逸出字元的%和_仍然起萬用字元作用

select username from gg_user where username like '%xiao/_%' escape '/';
select username from gg_user where username like '%xiao/%%' escape '/';

關於萬用字元

'A_Z': 所有以 'A' 起頭,另一個任何值的字原,且以 'Z' 為結尾的字串。 'ABZ' 和 'A2Z' 都符合這一個模式,而 'AKKZ' 並不符合 (因為在 A 和 Z 之間有兩個字原,而不是一個字原)。
'ABC%': 所有以 'ABC' 起頭的字串。舉例來說,'ABCD' 和 'ABCABC' 都符合這個套式。
'%XYZ': 所有以 'XYZ' 結尾的字串。舉例來說,'WXYZ' 和 'ZZXYZ' 都符合這個套式。
'%AN%': 所有含有 'AN' 這個套式的字串。舉例來說, 'LOS ANGELES' 和 'SAN FRANCISCO' 都符合這個套式。

在 SQL 中,可使用以下萬用字元:

萬用字元 描述
% 替代一個或多個字元
_ 僅替代一個字元
[charlist] 字元列中的任何單一字元

[^charlist]

或者

[!charlist]

不在字元列中的任何單

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.