select EMPLOYEE_ID,LAST_NAME,JOB_ID,DEPARTMENT_ID
from EMPLOYEES
where DEPARTMENT_ID = 90 ←條件設置,也可以寫last_name = 'whalen' 或者 hire_date = '1994-06-09'之類的,大小寫不區分(除非特別設定),日期預設格式:yyyy-mm-dd
select EMPLOYEE_ID,LAST_NAME,JOB_ID,DEPARTMENT_ID
from EMPLOYEES
where DEPARTMENT_ID >=90
也可以設定這種「比較運算子」,有:
- =
- >
- >=
- <
- <=
- <> 或 != 不等於
- between...and... (x between a and b)=(x <= a and x >= b)
- like 尋找指定的字元
- in 符合條例清單值 (where manager_id in (100,101,201))
- is null 是否為空值 (不可以用=NULL,除非set ansi_nulls off)
- is not null :不是空值
以下邏輯運算:
- x AND y:兩者都成立才回傳
- x OR y:任一成立即回傳
- NOT:「不是」時回傳(例如where name not mike,回傳名字不是mike的人)
Like萬用字元搜尋
%:表示0或多個字元,例如%s代表只要是S字尾即可、S%代表只要是S字頭即可、%S%代表只要有S即可
_:表示一個字元,例如_S%代表去找第二個字是S的字
[X-Y]:指定可以選擇的範圍,例如 [A-T]bbc 可以找到Abbc、cbbc等等,但不會搜出Xbbc
[^X-Y]:指定不可以選擇的範圍,例如[^A-T]bbc就不會出現Abbc等字樣
多條件時運算優先順序:
- Arithmetic operators
- Concatenation operator
- Comparison conditions
- is "not" null,like,'not' in
- 'not' between
- not
- and
- or
因為太難記了,所以記的最後三個跟善用括號就好啦~
還是算一下看看:
select LAST_NAME,JOB_ID,SALARY
from EMPLOYEES
where job_id = 'sa_rep'
or job_id = 'ad_pres'
and salary>15000
先算and:所以是(job_id = 'ad_pres' and salary>15000)中兩者都符合的先挑出來,之後再跟job_id = 'sa_rep'比對,其一OK即可:
LAST_NAME | JOB_ID | SALARY |
King | AD_PRES | 24000 |
Abel | SA_REP | 11000 |
Taylor | SA_REP | 8600 |
Grant | SA_REP | 7000 |
所以可以發現只有一個薪水超過15000的AD
select LAST_NAME,JOB_ID,SALARY
from EMPLOYEES
where(job_id = 'sa_rep'
or job_id = 'ad_pres')
and salary>15000
LAST_NAME | JOB_ID | SALARY |
King | AD_PRES | 24000 |
利用括號改變優先順序後的結果又不同了。
留言列表