Oracle BETWEEN & IN & NULL

 如果我想要在dept table 找 deptno 為20 或 30 的ename, hiredate資料

照之前學過可以使用or

query:

SELECT ename, hiredate FROM EMP

WHERE deptno = 20 

OR deptno = 30


不過還有一種方式是用in

是一樣的結果

SELECT ename, hiredate FROM EMP

WHERE DEPTNO IN (20,30)


如果想要找ename為FORD,SMITH,ALLEN,WARD,MARTIN的ename, hiredate資料
query:
SELECT ename, hiredate FROM EMP
WHERE ename IN ('FORD', 'SMITH', 'ALLEN', 'WARD', 'MARTIN')

如果想要找ename為FORD,SMITH,ALLEN,WARD,MARTIN的ename, hiredate資料
SELECT ename, hiredate FROM EMP
WHERE ename NOT IN ('FORD', 'SMITH', 'ALLEN', 'WARD', 'MARTIN')


接下來是BETWEEN
BETWEEN 會跟 AND 連用表示 在...之間
例如想找hiredate在1981/9/28~1982/12/9之間的資料

query:
SELECT * 
FROM emp
WHERE hiredate BETWEEN to_date('1981-09-28','yyyy-mm-dd') AND to_date('1982-12-09','yyyy-mm-dd')
EMPNOENAMEJOBMGRHIREDATESALCOMMDEPTNO
7839KINGPRESIDENT-1981/11/175000-10
7788SCOTTANALYST75661982/12/093000-20
7902FORDANALYST75661981/12/033000-20
7654MARTINSALESMAN76981981/09/281250140030
7900JAMESCLERK76981981/12/03950-30
7934MILLERCLERK77821982/01/231300-10

或是想找 sal 在 1000~2000之間的資料
query:
SELECT * FROM emp
WHERE sal BETWEEN 1000 AND 2000

如果想找sal 在1000~2000這個區間外的資料
query:
SELECT * FROM emp
WHERE sal NOT BETWEEN 1000 AND 2000

如果想找emp table 內 comm 是 null的資料
query:
SELECT * 
FROM emp
WHERE comm IS  NULL;

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 內 comm 不是 null的資料
query:
SELECT * 
FROM emp
WHERE comm IS NOT NULL;
EMPNOENAMEJOBMGRHIREDATESALCOMMDEPTNO
7499ALLENSALESMAN76981981/02/20160030030
7521WARDSALESMAN76981981/02/22125050030
7654MARTINSALESMAN76981981/09/281250140030
7844TURNERSALESMAN76981981/09/081500030

0 Comments:

張貼留言