Oracle学习:常用语句
一、select語句
Select 語句的整體形式:
select col1, col2… // 要查找的列名from table_name // 要搜索哪張表 where condition // 設置查找條件 group by col… // 設置分組 having condtion // 設置分組條件 order by col… // 設置排序下面舉例說明具體用法:
(1)查詢所有員工的所有記錄
(2)查詢 員工號,姓名,月薪,獎金,年薪
select empno, ename, sal, comm, sal*12 from emp;(3) 對例 (2) 使用別名
select empno, ename, sal as 工資, comm 獎金, sal*12 "年 薪" from emp;(4)查詢員工號,姓名,月薪,獎金,年薪,年收入
select empno, ename, sal 工資, comm 獎金, sal*12 "年 薪", sal*12+nvl(comm, 0) "年收入" from emp;//nvl(a, b),如果 a 為空,則取 b 的值; a 不為空,取 a 的值(5)查看員工表不同的部門編號
select distinct deptno from emp;//distinct去重,即沒有重復數據(7)輸出計算表達式 演示系統時間和偽表
select 3+20*5, sysdate from dual;//sysdate顯示當前時間, dual:偽表 為了滿足 SQL 語法,from 后必須跟表名二、條件過濾
基本結構:
select .... from table where condtion;具體用法如下:
(1)查詢 10 號部門的員工信息
(2)查詢員工名字為 KING 的員工信息
select * from emp where ename = 'KING'; //這里區分大小寫(3)查找薪水不等于 1250 員工的信息
select * from emp where sal != 1250; select * from emp where sal <> 1250;//這兩者等價(4) 查詢入職日期為 1981 年 11 月 17 日的員工信息
select * from emp where hiredate = '17-11 月-1981' ;//需要注意日期格式 NLS_DATE_FORMAT, 默認是 DD-MON-RR,也可以自己設定(5)查詢工資介于 1000-2000 之間的員工信息 演示 > >= < <=
select * from emp where sal>=1000 and sal<=2000; select * from emp where sal between 1000 and 2000;//兩者等價三、邏輯運算符: or and not
(1) 查詢 10 號部門或者 20 部門的員工信息
select * from emp where depton = 10 or depton = 20 ;(2) 查詢 10 號部門員工工資為 1300 的員工信息
select * from emp where depton = 10 and sal = 1300 ;(3)查詢 81 年 2 月(含 2 月)至 82 年 2 月(不含 2 月)
select * from emp where hiredate >= '01-2 月-81' and hiredate < '01-2 月-82' ;//這種情況無法使用 between and 進行查詢注意日期格式問題,注意月份單月不要在前面加 0,否則會報錯
(4)查詢獎金為空的員工信息為
(5)關于 and 和 or 短路問題的說明
對于 and 要把容易出現假的放在最右邊,因為 and 右側一旦為假,便不會在判斷 and 左側的真假,因此可以提高效率;同理,or 要把容易出現真的放在最右邊
(6)查詢 10 號部門和 30 號部門,工資為 1250 的員工信息
注意:在 where 條件表達式中有 or 的時候, 應該使用 ( ) 括起來 。
四、集合運算符: in 和not in
(1)查詢部門號是 10 或者 20 的員工信息
select * from emp where deptno in (10, 20, null);(2)查詢不是 10 和 20 號部門的員工信息
select * from emp where deptno not in (10, 20);注意:
select * from emp where deptno not in (10, 20, null);查不到記錄。因為上句等價于
select * from emp where deptno!=10 and deptno!=20 and deptno!=null;deptno!=null 等價于 null,因為 and,整個條件為假,按空條件查找,自然查不到記錄
總結:在集合中用 in,不在集合中用 not in
in 后面可以出現 null; not in 后面的集合當中不要出現 null。
五、like 模糊查找
(1)查詢員工首字母是 S 的員工信息
select * from emp where ename like 'S%';(2) 查詢員工編號為 79 開頭的員工信息
select * from emp where empno like '79%';(3)查詢名字為四個字母長度的員工信息
select * from emp where ename like '____';(4) 查詢員工姓名帶_的員工信息
select * from emp where ename like '%\_%' escape '\'; // escape '\' 表示 \ 是轉義字符總結
以上是生活随笔為你收集整理的Oracle学习:常用语句的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Qracle学习:初识oracle
- 下一篇: Qracle学习:排序