sql語句之where子句

如今的登陸都是把信息存在數據庫,而後把輸入的與數據庫內容進行匹配,同樣就登陸成功,不然不成功。驗證碼是爲了防止暴力破解,由於計算機可以自動匹配密碼,可是不能識別圖片上的字母,只有人能識別,因此匹配的速度會減慢。還有的會記錄登陸IP,若是IP頻繁變化就會進行提示。還有銀行會限制輸入次數。sql

做用

限制表中的數據返回  符合where後面的條件的數據就會被選中,不符合where條件的語句會被過濾掉數據庫

兩個極限條件

/*這是永真條件*/   (數據庫裏能夠有註釋,這裏是多行註釋)
where  1 = 1 ; (用一個等號判斷相不相等,由於這裏是不存在賦值的,沒有「= =」)
- -  這是永假條件(數據庫裏能夠有註釋,這裏是單行註釋,註釋符和註釋之間要有空格)
where  1 = 2 ;

 

演示:列出每一個員工的id 和salary   要求顯示符合salary等於1400的員工

select id, salary from s_emp where salary=1400

字符串條件的表達

演示:要求顯示first_name 是Carmen的員工,列出id  first_name salary 

select id, first_name, salary from s_emp where first_name='Carmen'

注意:必定要加’ ‘表明這是字符串值spa

常見的運算符

= 等於   != 不等於    >大於   < 小於    <= 小於等於   ................3d

 sql提供的運算符

表達一個閉區間[a , b] 

where  字段  between a  and  b ;  (字段在閉區間a到b內)blog

a  b的順序不能錯圖片

演示:寫程序查詢,把s_emp表中id  first_name salary  顯示;要求salary在[1450,2500 ] 中。

select id, first_name, salary from s_emp where salary between 1450 and 2500

      注意:不能夠把2500和1450調換位置,編譯不會有錯,但邏輯有錯)字符串

where  字段  in(值1,值2,值3)

這個字段的值等於其中的一個值(只要有一個等於就返回), 交換值的順序可能有影響,也可能沒有影響。若值的機率都同樣就沒有影響(就按一個規律寫(好比從小到大),這樣不容易遺漏)。若不同,則把機率高的值放在前面(人爲的),這樣查詢效率高(由於每一個數據都要挨個和給的值比較,只要有一個同樣就返回)io

演示:寫一個查詢,把s_emp表中部門標號是31或者32或者50 部門的員工id first_name  dept_id顯示出來

select idm first_name, dept_id from s_emp where dept_id in(31, 32, 50)

 

模糊查詢  like(像)+ 通配符

數據庫裏:編譯

    • 「%」爲通配符,表明0 - n個任意字符
    •  「-」表明一個任意字符

e.g.  李四 李斯  李思  李世民 (查找出姓李的)table

Where  name  like  ‘李%’;

e.g.  李小龍 小龍女  龍貓  (查找出全部帶龍的)

Where  name  like  ‘%龍%;
(找出中間帶龍的)where  name like  ‘_龍%’;

演示:查詢s_emp表中first_name,找出全部帶a(小寫a)的

select first_name from s_emp where first_name like '%a%'

 

數據庫中有一張表user_tables(數據字典,存的都是大寫)存了全部表的信息。例如s_emp  s_dept 等

desc user

演示:從user_tables中找出全部以‘S_’開頭的表名

注意:要對‘_’進行轉義處理,用‘\_’表示下劃線,再加escape  ‘ \ ’  表明是‘\’ 後面的內容進行轉義處理

select table_name from user_tables where table_name like 'S\_%'

NULL值的判斷

where  字段  is NULL ;

演示:把s_emp表中提成是10的員工的id  first_name commission_pct顯示出來

select id, first_name, commission_pct from s_emp where commission_pct=10

  

演示:把s_emp表中提成不是10的員工的id first_name  commission_pct顯示出來。

select id, first_name, commission_pct from s_emp where commission_pct!=10

按理來講,一共有25人,不爲10的人應該是20個的,可是這裏只有3個。這是由於,基本的斷定對空值是無效的,必須引入is  NULL對控制進行斷定因此要:

select id, first_name, commission_pct from s_emp where commoission_pct is NULL

      固然也能夠結合nvl,可是用is NULL是標準用法。

條件鏈接符號

  • and  邏輯與
  • or   邏輯或
  • not  

演示:(1)寫程序查詢,把s_emp表中id  first_name salary  顯示;要求salary在[1450,2500 ] 中。(between 。。。and。。。)

select id, first_name ,salary from s_emp where salary>=1450 and salary<=2500

用這個更加具備通用性,能夠是開區間。

(2)寫一個查詢,把s_emp表中部門標號是31或者32或者50 部門的員工id first_name  dept_id顯示出來(5.6.2裏的in(。。,。。,。。))

select id, first_name, dept_id from s_emp where dept_id-31 or dept_id-32 or dept_id=50

這裏三個都是等價的,不存在順序問題。

  • >  的對立面是  <=
  •  <  的對立面是  >=
  •  =  的對立面是  !=   ^=   < >  (都是不等於)
  •  between a  and  b  的對立面是  not between  a  and  b
  •  in  的對立面是  not  in
  •  like  的對立面是  not like
  •  is  null  的對立面是  is not  null(只有最後一個不用注意空值,上面的都要注意空值)

 演示:找出manager_id不是空的員工,列出id  first_name manager_id

select id, first_name, manager_id from s_emp where manager_id is not null

條件優先的問題  要優先的部分加括號

演示:(1)顯示員工salar  dept_id;

    要求工資大於1000且部門標號爲41的員工,或者部門標號爲42的員工  

select salary, dept_id from s_emp where salary>1000 and dept_id=41 or dept_id=42

(2)顯示員工salar  dept_id;

       要求部門標號爲41的員工,或者爲42的員工裏工資大於1000的

select salary, dept_id from s_emp where salary>1000 and (dept_id=41 or dept_id=42)

 

相關文章
相關標籤/搜索