oracle數據庫基礎(2)

DQL數據庫查詢語言

1,select語句最簡單的語法:
語法:
select * from 表名

*是統配符,表示所有的列 t表示表的別名
select * from emp t

查看部份列:

select 列名列表 from 表名;

–查看員工姓名,工作,薪水
select ename,job,sal from emp;
2,dual僞表:
當在查看的結果中不涉及到表時,可以用dual這張表
select 1+1 from dual;
select sysdate from dual;
select user from dual;
3,–算術運算符 ±*/

–查看員工姓名及年薪
select ename,sal12 from emp;
4,–列的別名
select ename,sal
12 as 年薪 from emp;-- as可加可不加
select ename,sal12 年薪 from emp;
select ename,sal
12 “年 薪” from emp;–當列的別名中有空格時,必須要加雙引號 不能用單引號
5,連接符||:

select ename||‘的年薪是:’||sal*12 “員工年薪信息” from emp;
在這裏插入圖片描述
6,order by排序:(order by 寫在select的最後面)
語法:
select 列名列表 from 表名 order by 排序列名(可以有多個,逗號隔開)

–1)升序(默認) order by 列名 【asc】
–2)降序: order by 列名 desc

–查看員工表薪水從低到高進行排序
select * from emp order by sal;

–查看員工表薪水從高到低進行排序
select * from emp order by sal desc;

–查詢薪水從高到低,入職時間從後往前進行排序
–注意:當涉及多列排序時,先按第一列進行排序,當第一列有相同時,再按第二列進行排序
select * from emp order by sal desc,hiredate desc;
7,where條件語句:
作用:選擇數據、過濾數據作用
語法:
select 列名列表 from 表名 where 條件 order by 排序列名
比較運算符和邏輯運算符:
–比較運算符 = > < >= <= != <>(不等於)
–邏輯運算符 and or not非

–查詢員工表中姓名是SMITH的員工信息
select * from emp where ename=‘SMITH’;
–查詢員工表中工資大於 2000的員工信息
select * from emp where sal>2000;
–查詢員工表中工資2000到5000之間的員工信息
select * from emp where sal>=2000 and sal<=5000;
–查詢員工表中不是部門20的員工信息
select * from emp where deptno<>20;
select * from emp where deptno!=20;
–查詢員工入職時間在1981年之後(包括1981年)的員工信息
select * from emp where hiredate>=to_date(‘1981-01-01’,‘yyyy-MM-dd’)
–查詢沒有獎金的員工信息
select * from emp where comm is null;
–查詢有獎金的員工信息
select * from emp where comm is not null;
select * from emp where not comm is null;
8,模糊查詢 :
1)between … and … --表示一個範圍 包含兩個邊界值
2) in 條件爲等於多個值時
3) like ( 模糊查詢代表關鍵字)

1)between … and …–表示一個範圍 包含兩個邊界值 ==> >= and <=
–查詢員工表中工資2000到5000之間的員工信息
select * from emp where sal>=2000 and sal<=5000;–等價於下面寫法
–注意一定要包含這兩個邊界值纔可以使用between … and …
select * from emp where sal between 2000 and 5000;
–查詢員工表中入職時間是1981年到1985年入職的員工信息
select * from emp where hiredate between to_date(‘1981-0101’,‘yyyy-mm-dd’)
and to_date(‘1985’)
select * from emp where hiredate between to_date(‘1981-01-01’,‘yyyy-mm-dd’)
and to_date(‘1985-12-31’,‘yyyy-mm-dd’);

  1. in 條件爲等於多個值時
    –查詢員工姓名是SMITH SCOTT JONES的員工信息
    select * from emp where ename=‘SMITH’ or ename=‘SCOTT’ or ename=‘JONES’;–等價於下面寫法
    select * from emp where ename in (‘SMITH’,‘SCOTT’,‘JONES’);

3)like 模糊查詢關鍵字
統配符:
% 表示匹配0到多個字符 ============= linux 中的 *
_ 表示匹配1個字符 ============= linux 中的 ?

–查詢姓名是S開頭的員工信息
select * from emp where ename like ‘S%’;
select * from emp where ename=‘S%’;–不能使用等號 錯誤寫法
–查詢姓名是包含M的員工信息
select * from emp where ename like ‘%M%’; --%S S結尾
–查詢姓名是第二個字符是M的員工信息
select * from emp where ename like 'M%’; --%M 倒數第二個是M
–查詢姓名是以5個字符組成的員工信息
select * from emp where ename like ‘_____’;
9,–distinct去重
–查看員工的所有工作
select distinct job from emp;–單列去重
–查看員工的所有工作以及領導編號
select distinct job,mgr from emp;–多列去重
10,–rownum僞列 表示行號
–表的別名:
select * from emp;

select ,sal12 “年薪” from emp;–錯誤寫法
–當select後面的列表項除了星號之外還有其他的列的時候,
–那麼星號前面必須要加表名或表的別名點,否則會報錯
select emp.,sal12 from emp;–正確寫法
select t.,sal12 from emp t;–正確寫法
select rownum,t.* from emp t;–正確寫法

–取部份行,分四種情況:
–1)查詢某表中前n條記錄
–2)查詢某表中第n條到m條記錄 (m>n n!=1)
–3)查詢某表中按照某列排序後的前n條記錄
–4)查詢某表中按照某列排序後的第n條到m條記錄 (m>n n!=1)

–1)查詢某表中前n條記錄 (最簡單)
–舉例:查詢員工表中前5條記錄
select * from emp where rownum<=5;
–2)查詢某表中第n條到m條記錄 (m>n n!=1)
–舉例:查詢員工表中第5條到第10條記錄
select * from emp where rownum>=5 and rownum<=10;–錯誤寫法
—查詢過程中使用rownum作爲where條件時要求:
–只能從第1條開始並且連續的行是可以查的,
—跟第1條不連續或者跳過某一行的條件是查詢有問題的
select * from emp where rownum=1 or rownum>=3;
select * from emp where rownum=1 or rownum=2 or rownum=4;
–舉例:查詢員工表中第5條到第10條記錄
–1) 先編號 設置rownum別名 寫成(select rownum 別名,t.* from 表名 t)
–2) 把前面的select 語句括號括起來當作一張表 (嵌套一層查詢)
再根據別名作爲條件取其中的部份行

select * from (select rownum nums,t.* from emp t) where nums>=5 and nums<=10;
–3)查詢某表中按照某列排序的前n條記錄
–舉例:查詢員工表中薪水從高到低的前10條記錄
–假如涉及到排序的,一定要先排序,當作一張表,嵌套一層select語句
–再根據條件查詢前10條記錄,通過rownum作爲條件進行查詢
select * from (select * from emp order by sal desc) where rownum<=10;
–4)查詢某表中按照某列排序的第n條到m條記錄 (m>n n!=1)
–舉例:查詢員工表中薪水從高到低的第5名到第10名記錄
–1)假如涉及到排序的,一定要先排序,當作一張表,嵌套一層select語句
–2)再編號設置rownum別名 寫成(select rownum 別名,t.* from (select排序語句) t)
–3)再根據別名作爲條件取其中的部份行 嵌套兩層select語句
select * from (select rownum nums,t.* from (select * from emp order by sal desc) t)
where nums>=5 and nums<=10;
11,–常用函數
1)轉換函數 to_date to_char to_number
select to_date(‘1998-10-11’,‘yyyy-mm-dd’) from dual;
–轉化爲字符串,常用用法:to_char獲取日期當中的年份yyyy,月份mm,日dd
select * from emp;
–計算員工入職年份
select ename,to_char(sysdate,‘yyyy’)-to_char(hiredate,‘yyyy’) “入職年份” from emp;
select ename,(sysdate-hiredate)/365 “入職年份” from emp;
–計算年齡
select to_char(sysdate,‘yyyy’)-to_char(to_date(‘1981-10-01’,‘yyyy-mm-dd’),‘yyyy’) “年齡” from dual;
select to_number(‘9,999’,‘0,000’) from dual;–把字符串轉換爲數字類型

2)空值相關函數 nvl
–null值相關的函數
–查詢員工姓名及年薪(包括獎金)
select ename,sal12+comm from emp;–結果會有空值
–注意:任何值跟null值進行計算,得到的結果肯定是null值
–nvl(參數1,參數2)表示當參數1這一列爲空時用參數2代替
select ename,sal
12+nvl(comm,0) “年薪” from emp;

3)round四捨五入函數 trunc截取
–round四捨五入函數 trunc截取
select ename,round((sysdate-hiredate)/365,2) “入職年份” from emp;
select ename,trunc((sysdate-hiredate)/365,2) “入職年份” from emp;
12,分組查詢(重點):關鍵字group by
–分組函數(聚合函數): 主要應用於統計

–1)count 統計行數 count()常用
select count(
) “員工數” from emp;–select count(1) from emp
select count() “學生數” from student;
–2)sum 求和 (數字累計) sum(列名)
select sum(sal) “月付薪水” from emp;
select sum(grade) “學生考試總分” from score;
–3)avg 求平均值 avg(列名)
select avg(grade) “總平均分” from score;
select avg(sal) “員工平均薪水” from emp;
–4)max 求最大值 max(列名)
select max(sal) “最高薪水” from emp;
select max(sage) “最大年齡” from student;
–5)min 求最小值 min(列名)
select min(sal) “最少薪水” from emp;
select min(sage) “最小年齡” from student;
–分組查詢中有where條件
–查詢入職年限超過30年的員工數
select count(
) “員工數” from emp where (sysdate-hiredate)/365>30;
–查詢年齡大於18歲的學生數
select count(*) “學生數” from student where sage>18;

–group by 分組關鍵字
–group by 語法規則:
–select後面列除了分組函數之外,只能寫group by 後面的列,而且只能少不能多,
其他的列是不允許寫在select後面
–查詢部門人數超過5個人的部門編號

select deptno from emp group by deptno having count(*)>5;

–查詢部門平均工資超過2000的部門編號
select deptno,avg(sal) from emp group by deptno having avg(sal)>2000;
–查詢部門MANAGER平均工資超過2500的部門編號
select deptno from emp where job=‘MANAGER’ group by deptno having avg(sal)>2500;
select * from score;
–查詢每門課平均分超過85分的課程編號
select cid from score group by cid having avg(grade)>85;
–查詢課程編號是以4結尾的且平均分超過85分的課程編號
select cid from score where trim(cid) like ‘%4’ group by cid having avg(grade)>85;
–查詢每個學生最高分超過90分的學生編號
select sid from score group by sid having max(grade)>90
–查詢學生表中男生人數超過5個人的班級編號
select classno from student1 where ssex=‘男’ group by classno having count(*)>5;
–查詢學生表中各個班最小年齡小於17歲的班級編號
select classno from student1 group by classno having min(sage)<17;
13,–子查詢:(嵌套查詢)
–應用場景:
–1.一條查詢語句的查詢條件依賴另外一條查詢語句的查詢結果。
–2.一條查詢語句的查詢結果是作爲另外一條查詢語句的查詢表(查詢依據)。
–子查詢查詢結果有兩種情況:1)單行 > < <= != 2)多行 in

–查詢最高工資員工姓名
select ename from emp where sal=(select max(sal) from emp);
–查詢比SMITH工資要高的員工信息
select * from emp where sal>(select sal from emp where ename=‘SMITH’)
–查詢學生表中年齡最大的學生姓名
select sname from student where sage=(select max(sage) from student);
–查詢比部門20人數要多的各個部門編號及人數
select deptno,count() “人數” from emp group by deptno having count()>
(select count(*) from emp where deptno=20);
–查詢每個學生平均分超過所有學生平均分的學生編號
select sid from score group by sid having avg(grade)>(select avg(grade) from score);

14,多表關聯:
1)內聯接
2)外聯接(左外聯接,右外聯接,全聯接)

–什麼情況下用到多表關聯:
–1)查詢結果涉及到多張表,需要用到多表關聯
–2)查詢條件涉及到多張表,一般也會用到多表關聯

內聯接
–語法(兩張表的關聯)
–特點:
–關聯字段匹配到數據纔會顯示出來,沒有匹配到的不會顯示,例如:關聯字段爲空值的數據行是不會顯示出來,關聯後可能會有部份數據沒有顯示出來
第一種語法:
select 列名列表 from 表1 inner join 表2 on 表1.關聯字段=表2.關聯字段
select 列名列表 from 表1 t inner join 表2 k on t.關聯字段=k.關聯字段

例如:查詢員工姓名及部門名稱
select t.ename,d.dname from emp t inner join dept d on t.deptno=d.deptno;
–準備數據
create table emp_temp as select * from emp;
create table dept_temp as select * from dept;
insert into emp_temp select 8000,‘zhangsan’,job,mgr,hiredate,sal,comm,null
from emp where empno=7369;
insert into dept_temp values(50,‘market’,‘shanghai’)

–分析以下數據
select t.ename,d.dname from emp_temp t inner join dept_temp d on t.deptno=d.deptno;
第二種語法:
select 列名列表 from 表1,表2 where 表1.關聯字段=表2.關聯字段

–外聯接:
–左外聯接
–特點:
以左表爲主,左表數據全部顯示,右表有匹配到的匹配顯示,沒有匹配到空值顯示
–語法
select 列名列表 from 表1 left join 表2 on 表1.關聯字段=表2.關聯字段
select 列名列表 from 表1 t left join 表2 k on t.關聯字段=k.關聯字段
–例如:查詢所有員工姓名及部門名稱
select t.ename,d.dname from emp_temp t left join dept_temp d on t.deptno=d.deptno;

–右外聯接
–特點:
以右表爲主,右表數據全部顯示,左表有匹配到的匹配顯示,沒有匹配到空值顯示
–語法
select 列名列表 from 表1 right (outer) join 表2 on 表1.關聯字段=表2.關聯字段
select 列名列表 from 表1 t right (outer) join 表2 k on t.關聯字段=k.關聯字段
–例如:查詢所有員工姓名及部門名稱
select t.ename,d.dname from dept_temp d right join emp_temp t on t.deptno=d.deptno;

–全聯接(特點:即是左關聯又是右關聯,左右兩表都全部顯示)

–語法
select 列名列表 from 表1 full (outer) join 表2 on 表1.關聯字段=表2.關聯字段
select 列名列表 from 表1 t full (outer) join 表2 k on t.關聯字段=k.關聯字段

–例如:查詢所有員工以及所有部門信息
select t.,d. from dept_temp d full outer join emp_temp t on t.deptno=d.deptno;
查詢學生姓名以及班級名稱:
select s.sname,c.cname from student1 s inner join class1 c on s.classno=c.cno;

–查詢學生姓名,課程編號及分數
select s.sname,sc.cid,sc.grade from student s inner join score sc on s.sid=sc.sid;

–查詢所有學生姓名,課程編號及分數(注意有所有的文字一般用外鏈接)
select s.sname,sc.cid,sc.grade from student s left join score sc on s.sid=sc.sid;

–三表關聯(兩兩關聯)
–語法:
select 列名列表 from 表1 inner join 表2 on 表1.關聯字段=表2.關聯字段 inner join
表3 on 表3.關聯字段=表1.關聯字段 [ where …]
select 列名列表 from 表1 left join 表2 on 表1.關聯字段=表2.關聯字段 left join
表3 on 表3.關聯字段=表1.關聯字段
–查詢學生姓名,課程名稱,分數
select s.sname,c.cname,sc.grade from student s inner join score sc on s.sid=sc.sid inner join course c on c.cid=sc.cid;
–查詢所有學生姓名,課程名稱,分數
select s.sname,c.cname,sc.grade from student s left join score sc on s.sid=sc.sid left join course c on c.cid=sc.cid;
15,總結:
1) 理解表結構,主鍵 外鍵 各個列的意思(基本)
2)分析題目意思:
查詢結果和查詢條件分別是什麼?
(是否涉及到多張表,如果是,基本上多表關聯
有沒有涉及分組查詢的字眼:最大,最小,平均,和,人數)
如果有涉及分組查詢的字眼,基本上需要分組查詢,確定有沒有分組函數的條件
如果有,確定分組的列是什麼?

3)如果既有多表關聯又有分組查詢,那麼一定要先寫分組查詢,因爲分組查詢語法是受限制的
分組查詢先寫好(一般一張表),括號括起來,當作一張表
再去關聯另外表

發佈了25 篇原創文章 · 獲贊 12 · 訪問量 1757
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章