一、多表查詢
(一)簡單多表查詢
1、多表查詢的機制 1)SQL:
SELECT * FROM emp; --14條記錄 SELECT * FROM dept;--4條記錄 SELECT * FROM emp,dept;--顯示56條數據??為什麼
SELECT a.ename,a.sal,b.dname FROM emp a ,dept b WHERE a.deptno = b.deptno;部門號匹配即可。 2)顯示部門號為10 的部門名、員工名和工資
SELECT a.ename,a.sal,b.dname FROM emp a ,dept b WHERE a.deptno = b.deptno and a.deptno=10;
SELECT a.ename,a.sal,b.grade FROM emp a, salgrade b WHERE a.sal between b.losal and b.hisal;
SELECT a.ename,a.sal,b.dname FROM emp a ,dept b WHERE a.deptno = b.deptno order by b.dname;
(二)自連接
1、含義:自連接是指在同一張表的連接查詢,即把一張表看成 2 張表。
2、實例 1)顯示員工Ford的上級領導的姓名
SELECT a.* FROM emp a WHERE a.empno = (SELECT a.mgr FROM emp a WHERE a.ename = 'FORD') ;分析:
先查詢出FORD 的領導的員工編號。
SELECT distinct a.ename,b.ename,b.* FROM emp a, emp b WHERE a.mgr = b.empno;--13條數據 SELECT distinct a.ename,b.ename,b.* FROM emp a, emp b WHERE a.mgr = b.empno(+);--14條數據
【+】寫在右邊,表明是左外連接,左邊的表列全部顯示。
(三)子查詢*
1、含義: 1)子查詢是指嵌入其他sql語句中的 select語句,也叫嵌套查詢 2)單行子查詢:單行子查詢是指只返回一行數據的子查詢語句。 3)多行子查詢:多行子查詢指返回多行數據的子查詢。
SELECT a.* FROM emp a WHERE a.deptno = (SELECT a.deptno FROM emp a WHERE a.ename='SMITH' );
分析
注意:where 子查詢中,不能使用order by 排序。
SELECT a.* FROM emp a WHERE a.deptno = (SELECT a.deptno FROM emp a WHERE a.ename='SMITH' order by a.empno);
報錯,“缺失右括號” 2)查詢和10號部門的工作崗位相同的雇員的名字、崗位、工資、部門號 (多行子查詢:“in”)
SELECT a.ename,a.job,a.sal,a.deptno FROM emp a WHERE a.job in (SELECT distinct a.job FROM emp a WHERE a.deptno = 10) ;分析:
SELECT a.* FROM emp a WHERE a.sal > (SELECT max(a.sal) FROM emp a WHERE a.deptno=30);方法2:使用 all
SELECT a.* FROM emp a WHERE a.sal >all (SELECT a.sal FROM emp a WHERE a.deptno=30);
SELECT a.* FROM emp a WHERE a.sal > (SELECT min(a.sal) FROM emp a WHERE a.deptno=30);方法2:使用 any
SELECT a.* FROM emp a WHERE a.sal >any (SELECT a.sal FROM emp a WHERE a.deptno=30);
SELECT a.* FROM emp a WHERE (a.deptno,a.job) = (SELECT a.deptno,a.job FROM emp a WHERE a.ename='SMITH' ); SELECT a.* FROM emp a WHERE (a.deptno,a.job) in (SELECT a.deptno,a.job FROM emp a WHERE a.ename='SMITH' );這邊“=”與 in 效果一致。
SELECT a.ename,a.sal,b.avg_sal,a.deptno FROM emp a ,(SELECT avg(a.sal) as avg_sal,a.deptno as deptno FROM emp a group by a.deptno) b WHERE a.deptno = b.deptno and a.sal > b.avg_sal;分析:
SELECT a.ename,a.sal,t.max_sal , a.deptno FROM emp a, (SELECT max(a.sal) as max_sal,a.deptno as deptno FROM emp a group by a.deptno) t WHERE a.deptno = t.deptno and a.sal = t.max_sal;
得到各個部門的最高工資,作為一個臨時表
利用emp 與臨時表多表查詢
方法2:
SELECT a.* FROM emp a WHERE a.sal = (SELECT max(b.sal) FROM emp b WHERE b.deptno = a.deptno ) ;這種方法,無法顯示子查詢中的列數據,有缺陷。 3)顯示每個部門的信息和人員數量
SELECT a.*,t.count_empno FROM dept a, (SELECT count(a.empno) as count_empno,a.deptno FROM emp a group by a.deptno) t WHERE a.deptno = t.deptno(+);先獲取每個部門的人員數量,作為一個臨時表
create table temp# as select empno, ename from emp where ename like 'J%';
2)蠕蟲復制
insert into myTest (empno,ename,job) select empno,ename,job from myTest;3)子查詢更新數據
update emp set (empno,ename,job) = (select empno,ename,job from emp where ename = 'SMITH') where ename='SCOTT';
(四)合並查詢
1、UNION:並集去重,排序 1)解釋:該操作符用於取得兩個結果集的並集。當使用該操作符時,會自動去掉結果集中重復行。
SELECT a.* FROM emp a WHERE a.sal> 2500 UNION SELECT b.* FROM emp b WHERE b.job='MANAGER';
SELECT a.* FROM emp a WHERE a.sal> 2500 UNION ALL SELECT b.* FROM emp b WHERE b.job='MANAGER';
4、MINUS:差集,總之不會顯示第二個集合中的數據 1)有交集的情況:
2)沒有交集的情況:
二、分頁
(一)其他數據庫的分頁
1、MySQL
1)語法:
select * from 表名 where 條件 limit 從第幾條,取幾條 select * from emp where empno=1234 limit 1,3;
2、Sql server 1)語法:
select top 2 * from 表名 where id not in (select top 4 id from 表名 where 條件) 排除前4條,再取2條,實際上是取5-6條。
SELECT rownum, emp.* FROM emp where rownum <6; SELECT * FROM (SELECT t.*, rownum num FROM (SELECT * FROM emp) t WHERE rownum < 10) t2 WHERE t2.num > 4;
分析:rownum 不能直接使用大於號(>)
SELECT * FROM (SELECT t.*, rownum num FROM (SELECT a.* FROM emp a order by a.hiredate ) t WHERE rownum < 10) t2 WHERE t2.num > 7;排序等特殊處理等情況,請在最內層處理 2)測試,模擬10萬的數據
創建表與數據:
create table myTest as select empno,ename,job from emp;自我復制(蠕蟲復制)
insert into myTest (empno,ename,job) select empno,ename,job from myTest;測試:
SELECT * FROM (SELECT t.*, rownum num FROM (SELECT * FROM myTest ) t WHERE rownum < 51010) t2 WHERE t2.num > 50997;
三、內、外連接
(一)內連接 1、概述:內連接就是利用where 子句對2張表形成的笛卡爾集進行篩選。 2、格式 1)普通:
語法:select 列名... from 表1 表2.. where 條件
SELECT a.ename,a.deptno,b.dname FROM emp a,dept b WHERE a.deptno = b.deptno;2)【inner】 Join on
SELECT a.ename,a.deptno,b.dname FROM emp a inner join dept b on (a.deptno = b.deptno)
2、左外連 1)特點:left join
SQL:
SELECT a.id,a.name,b.grade FROM stu a,exam b WHERE a.id = b.id;只顯示2條數據,沒有成績的學生不會顯示
SELECT a.id,a.name,b.grade FROM stu a,exam b WHERE a.id = b.id(+); SELECT a.id,a.name,b.grade FROM stu a left join exam b on( a.id = b.id);分析: 內連接的時候,需要2張表都匹配上的記錄 左外連接:stu(左表) 表中與exam 中沒有匹配的,也需要顯示。
SQL:
SELECT a.id,a.name,b.grade FROM stu a,exam b WHERE a.id(+) = b.id; SELECT a.id,a.name,b.grade FROM stu a right join exam b on( a.id = b.id);分析:
4、完全外連 1)特點: full outer join
SQL:
SELECT a.id,a.name,b.grade FROM stu a full join exam b on( a.id = b.id); SELECT a.id,a.name,b.grade FROM stu a full outer join exam b on( a.id = b.id);分析:
5、總結: 1)左、右外連可以相互轉換,只需要換表的位置即可。
SELECT a.id,a.name,b.grade FROM stu a right join exam b on(a.id = b.id); SELECT a.id,a.name,b.grade FROM exam b left join stu a on( a.id = b.id);2)小結
6、練習:
1)列出部門名稱和這些部門員工的員工信息,同時列出那些沒有員工的部門
SELECT b.dname,a.* FROM emp a right join dept b on (a.deptno = b.deptno) order by b.deptno; SELECT b.dname,a.* FROM dept b left join emp a on (a.deptno = b.deptno) order by b.deptno; SELECT b.dname,a.* FROM emp a, dept b WHERE b.deptno = a.deptno(+) order by b.deptno; SELECT b.dname,a.* FROM emp a, dept b WHERE a.deptno(+) = b.deptno order by b.deptno;
四、練習
1、顯示雇員總數,以及獲得補助的雇員數
SELECT count(*) ,count(comm) FROM emp;
SELECT count(distinct mgr) FROM emp;
SELECT avg(a.sal),a.deptno,a.job FROM emp a GROUP BY cube(a.deptno,a.job );cube:立方體函數,對deptno分組,job分組,對(deptno、job)分組