Oracle WHERE & AND

 如果想取得在emp table內 job為SALESMAN的資料

query如下:

SELECT *

FROM emp

WHERE job = 'SALESMAN'

可得到以下內容

EMPNOENAMEJOBMGRHIREDATESALCOMMDEPTNO
7499ALLENSALESMAN76981981/02/20160030030
7521WARDSALESMAN76981981/02/22125050030
7654MARTINSALESMAN76981981/09/281250140030
7844TURNERSALESMAN76981981/09/081500030

提醒,column, table 名稱不分大小寫
但資料內容是有分大小寫的

所以如果你輸入以下query
SELECT *

FROM emp

WHERE job= 'salesman'

會找不到資料



如果想取得在emp table內 job不為SALESMAN的資料
query:
SELECT *

FROM emp

WHERE job != 'SALESMAN'
會得到以下結果
EMPNOENAMEJOBMGRHIREDATESALCOMMDEPTNO
7839KINGPRESIDENT-1981/11/175000-10
7698BLAKEMANAGER78391981/05/012850-30
7782CLARKMANAGER78391981/06/092450-10
7566JONESMANAGER78391981/04/022975-20
7788SCOTTANALYST75661982/12/093000-20
7902FORDANALYST75661981/12/033000-20
7369SMITHCLERK79021980/12/17800-20
7876ADAMSCLERK77881983/01/121100-20
7900JAMESCLERK76981981/12/03950-30
7934MILLERCLERK77821982/01/231300-10


如果想取得在emp table內 job為SALESMAN的資料以及sal大於等於1500的資料
query:
SELECT *

FROM emp

WHERE job= 'SALESMAN'

AND sal >= 1500

EMPNOENAMEJOBMGRHIREDATESALCOMMDEPTNO
7499ALLENSALESMAN76981981/02/20160030030
7844TURNERSALESMAN76981981/09/081500030



也可以比較column的值,
例如我想找comm > sal 的資料
query:
SELECT *

FROM emp

WHERE comm > sal

EMPNOENAMEJOBMGRHIREDATESALCOMMDEPTNO
7654MARTINSALESMAN76981981/09/281250140030



小結:
查詢的順序
1. SELECT
2. FROM
3. WHERE
4. AND
...

0 Comments:

張貼留言