一,select
select 用於從數據看查詢數據。語法:
select field1,filed2,.. . from tablename [where condition]
舉例:
-- 查詢所有員工的名字和雇員號 select e.ename,e.empno from emp e;
* 通配符表示查詢所有字段。如果要查特定字段時,不要使用*,影響查詢效率
select * from emp;
distinct(修飾多字段時,多個字段的值都不一樣才保留)
-- 查詢公司的工種 select distinct e.job from emp e;
where字句
where 表示查詢的條件。
[1] =,!= ,<>,<,>,<=,>= 關系運算符
<> 表示不等於
-- 把部分10的雇員查詢出來 select * from emp where deptno = 10; -- 把名稱為smith的雇員 select e.* from emp e where e.ename = 'SMITH'; -- 查詢底薪大於等於1000的員工 select e.* from emp e where e.sal >= 1000; select e.* from emp e where e.sal <> 800
any/some/all (list)
any/some(list) 滿足list列表中的任意一個條件
all(list) 滿足list列表的中所有條件
-- 查詢薪資大於1000或者薪資大於800的雇員 select e.* from emp e where e.sal > some(1000,800); -- 查詢薪資大於1000 select e.* from emp e where e.sal > all(1000,800);
[2] null
null 在sql中表示的是不確定 => 可以認為沒有值
-- 查詢沒有津貼的雇員 select e.* from emp e where e.comm is null select e.* from emp e where e.comm is not null
[3] between x and y
表示一個值位於[x,y]區間,x/y 一般都是數字
-- 查詢薪資在1000-5000之間的雇員 select e.* from emp e where e.sal between 1000 and 5000 -- 查詢薪資在(3000,5000]之間的雇員 select e.* from emp e where e.sal between 3000.01 and 5000
[4] in/not in list
表示字段值是否在list列表中
-- 查詢部分號是10和20的員工 select e.* from emp e where e.deptno in(10,20); select e.* from emp e where e.deptno not in(10,20); -- 查詢薪資是1000,2000,5000的員工 select e.* from emp e where e.sal in (1000,2000,5000);
[5] 模糊查詢
like 關鍵字用於模糊查詢,其中
%:表示任意字符出現多次(含0次),
_:表示任意字符出現1次。
escape(‘x’) 表示指定轉義字符為x,一般指定為\
-- 查詢名字是c開頭的雇員 select e.* from emp e where e.ename like 'c%'; -- 查詢名字中第二個字母是M的雇員 select e.* from emp e where e.ename like '_M%' -- 查詢名字中含有M的雇員 select e.* from emp e where e.ename like '%M%'; -- 查詢名字中含有%的雇員 select e.* from emp e where e.ename like '%\%%' escape('\');
二,復雜查詢
where 后面的條件可以跟多個通過and 或者 or 連接
and:且、並且
or: 或、或者
-- 查詢部門10且薪資大於等2000的雇員 select e.* from emp e where e.deptno = 10 and e.sal >= 2000; -- 查詢名字中含M且薪資大於1000的雇員 select e.* from emp e where e.ename like '%M%' and e.sal > 1000 -- 查詢部門在10或20的雇員 select e.* from emp e where e.deptno = 10 or e.deptno = 20;
思考:查詢條件的順序對查詢速度是否有影響?
分析:
and 表示且,條件越多,檢索的數據量越來越少
or 表示或,條件越多,檢索的數據量越來越多
where 條件的執行順序從后向前
-- 優化后的sql select e.* from emp e where e.sal>=2000 and e.deptno = 10
AND: 把檢索結果較少的條件放到后面(先執行)
三,計算字段
我們經常需要把數據庫中檢索出來的信息進行再加工,允許的操作+、-、*、/。通過四個運算得到新的字段(計算字段)。
計算字段在數據表中不存在
-- 查詢出每個雇員的月薪(收入) select e.ename,e.sal+e.comm as "收入",e.deptno from emp e
運算結果:
注意:很多記錄中的comm是null,表示不確定的值,經常四則運算后的值也不確定。
當遇到字段時null時,可以通過nvl函數把null轉化便於運算的類型
-- nvl函數優化 select e.ename,e.sal+nvl(e.comm,0) "收入",e.deptno from emp e
四,函數
函數一般是在數據上執行的,它給數據的轉換和處理提供了方便。只是將取出的數據進行處理,不會改變數據庫中的值。
函數根據處理的數據分為單行函數和聚合函數(組函數)
組函數又被稱作聚合函數,用於對多行數據進行操作,並返回一個單一的結果,組函數僅可用於選擇列表或查詢的having子句
單行函數對單個數值進行操作,並返回一個值
五,字符相關
常用計算字符舉例
-- dual用於測試 select * from dual; -- 1.字符串連接 select concat('aa','12') from dual; select 'aa'||'12' from dual; -- 2.首字母大寫 select initcap('abc') from dual; --- 把大寫轉化小寫 select lower('ABc') from dual; select upper('abc') from dual; -- 把所有員工的姓名小寫輸出 select lower(e.ename),e.empno from emp e -- 3.填充字符lpad/rpad select lpad('sxt',5,'*') from dual; select rpad('sxt',5,'*') from dual; -- 4.去掉空白字符 select ' kallen' from dual; select ltrim(' kallen',' ') from dual; select rtrim(' kallen ',' ') from dual; -- trim 刪除左右兩邊的字符 select trim('a' from 'abc') from dual; -- 5.求子串 substr(str,loc,len)-->loc從1開始 select substr('abcd',2,2) from dual; -- 6.查找字符串 /* 如果找到返回>=1的索引;如果沒找到返回0 */ select instr('abcd','b') from dual; -- 7.求長度 select length('abcd') from dual;