2023_10_10_MYSQL_DAY_02_筆記 #在 FROM 子句中使用子查詢 SELECT a.ename, a.sal, a.deptno, b.salavg FROM emp a, (SELECT deptno, AVG(sal) salavg FROM emp GROUP BY de ...
2023_10_10_MYSQL_DAY_02_筆記
#在 FROM 子句中使用子查詢
SELECT a.ename, a.sal, a.deptno, b.salavg
FROM emp a, (SELECT deptno, AVG(sal) salavg
FROM emp
GROUP BY deptno) b
WHERE a.deptno = b.deptno AND a.sal > b.salavg;
#子查詢里的空值處理
SELECT ename
FROM emp
WHERE empno NOT IN (SELECT IFNULL (mgr,0) FROM emp);
#多列子查詢
SELECT deptno,ename,hiredate
FROM emp
WHERE (deptno,hiredate) IN (SELECT deptno ,MIN(hiredate)
FROM emp
GROUP BY deptno);
#課堂練習41
SELECT ename,job
FROM emp
WHERE job = ANY (SELECT job FROM emp WHERE deptno=10)
AND deptno <> 10;
#課堂練習40
SELECT ename,hiredate
FROM emp
WHERE hiredate > ALL (SELECT hiredate FROM emp WHERE deptno=10)
AND deptno <> 10;
#課堂練習39
SELECT ename,hiredate
FROM emp
WHERE hiredate > ANY (SELECT hiredate FROM emp WHERE deptno=10)
AND deptno <> 10;
#多行子查詢 all操作符
SELECT empno, ename, job, sal
FROM emp
WHERE sal > ALL (SELECT sal FROM emp WHERE deptno= 20)
AND deptno <> 20;
SELECT empno, ename, job, sal
FROM emp
WHERE sal < ALL (SELECT sal FROM emp WHERE deptno= 20)
AND deptno <> 20;
#多行子查詢 any操作符
SELECT ename,sal,empno
FROM emp
WHERE empno < ANY (SELECT mgr FROM emp ORDER BY mgr DESC);
SELECT ename,sal,empno
FROM emp
WHERE empno > ANY (SELECT mgr FROM emp ORDER BY mgr DESC);
SELECT ename,sal,empno
FROM emp
WHERE empno = ANY (SELECT mgr FROM emp ORDER BY mgr DESC);
#多行子查詢 in操作符
SELECT ename,sal
FROM emp
WHERE empno IN (SELECT mgr FROM emp);
#課堂練習38
SELECT deptno,AVG(sal)
FROM emp
GROUP BY deptno
HAVING AVG(sal) > (SELECT AVG(sal) FROM emp WHERE deptno=10);
#課堂練習37
SELECT ename,sal,dname
FROM emp e,dept d
WHERE e.`deptno`=d.`deptno`
AND sal > (SELECT sal FROM emp WHERE ename='SMITH')
AND d.`loc`='CHICAGO';
#課堂練習36
SELECT ename,hiredate
FROM emp
WHERE hiredate = (SELECT MIN(hiredate) FROM emp);
# HAVING子句中使用子查詢
SELECT deptno, MIN(sal)
FROM emp
GROUP BY deptno
HAVING MIN(sal) > (SELECT MIN(sal) FROM emp WHERE deptno = 20);
#子查詢中使用組函數
SELECT ename,job,sal
FROM emp
WHERE sal = (SELECT MIN(sal) FROM emp);
#單行子查詢語句
SELECT ename,job
FROM emp
WHERE job = (SELECT job FROM emp WHERE empno=7369)
AND sal > (SELECT sal FROM emp WHERE empno=7876);
#子查詢 單行子查詢
SELECT ename
FROM emp
WHERE sal > (SELECT sal FROM emp WHERE ename='JONES');
#課堂練習35
SELECT ename,hiredate,deptno
FROM emp
LIMIT 0,5; #第1頁
SELECT ename,hiredate,deptno
FROM emp
LIMIT 5,5; #第2頁
SELECT ename,hiredate,deptno
FROM emp
LIMIT 10,5; #第3頁
#課堂練習34
SELECT e.deptno,dname,COUNT(*),AVG(sal)
FROM emp e,dept d
WHERE e.`deptno`=d.`deptno`
GROUP BY e.deptno,dname
HAVING COUNT(*) > 2 AND AVG(sal) > 2000
ORDER BY COUNT(*);
#七個子句的查詢
SELECT job,SUM(sal)
FROM emp
WHERE job NOT LIKE 'SALES%'
GROUP BY job
HAVING SUM(sal) > 5000
ORDER BY SUM(sal)
LIMIT 0,2;
#分頁查詢
SELECT empno,ename,deptno,sal
FROM emp
ORDER BY empno
LIMIT 5,5; #第2頁
SELECT empno,ename,deptno,sal
FROM emp
ORDER BY empno
LIMIT 0,5; #第1頁
#課堂練習33
SELECT e.deptno,dname,COUNT(*)
FROM emp e,dept d
WHERE e.`deptno`=d.`deptno`
GROUP BY e.deptno,dname
HAVING COUNT(*) > 2;
總結:
1、WHERE子句用來過濾分組之前的記錄,不能使用組函數
2、HAVING子句用來過濾分組之後的記錄,可以使用組函數
#6個子句的查詢
SELECT job,SUM(sal)
FROM emp
WHERE job NOT LIKE 'SALES%'
GROUP BY job
HAVING SUM(sal) > 5000
ORDER BY SUM(sal);
查詢語句執行過程:
1、通過FROM子句中找到需要查詢的表;
2、通過WHERE子句進行非分組函數篩選判斷;
3、通過GROUP BY子句完成分組操作;
4、通過HAVING子句完成組函數篩選判斷;
5、通過SELECT子句選擇顯示的列或表達式及組函數;
6、通過ORDER BY子句進行排序操作。
#HAVING 子句
SELECT deptno,COUNT(empno)
FROM emp
GROUP BY deptno
HAVING COUNT(empno) > 3;
SELECT deptno,MAX(sal)
FROM emp
GROUP BY deptno
HAVING MAX(sal) > 2900;
#課堂練習32
SELECT COUNT(*),MAX(sal),MIN(sal),d.`loc`
FROM emp e,dept d
WHERE e.`deptno`=d.`deptno` AND d.`loc`='CHICAGO'
GROUP BY d.`loc`;
#課堂練習31 自連接
SELECT e2.`empno` 經理編號,e2.`ename` 經理姓名,COUNT(*)
FROM emp e,emp e2
WHERE e.`mgr`=e2.`empno`
GROUP BY e2.`empno`,e2.`ename`;
#課堂練習30
SELECT d.`deptno`,d.`dname`,e.`job`,COUNT(empno),MAX(sal),MIN(sal)
FROM emp e,dept d
WHERE e.`deptno`=d.`deptno`
GROUP BY d.`deptno`,d.`dname`,e.`job`;
#多表查詢分組查詢
SELECT d.`deptno`,d.`dname`,COUNT(empno),MAX(sal)
FROM emp e,dept d
WHERE e.`deptno`=d.`deptno`
GROUP BY d.`deptno`,d.`dname`;
#按多列分組查詢
SELECT deptno,job,SUM(sal)
FROM emp
GROUP BY deptno,job;
#分組子句
SELECT job,COUNT(empno)
FROM emp
GROUP BY job;
SELECT deptno,AVG(sal)
FROM emp
GROUP BY deptno;
#課堂練習29
SELECT COUNT(*),MAX(sal),MIN(sal)
FROM emp
WHERE deptno=30;
#課堂練習28
SELECT (sal+IFNULL(comm,0))*12 年收入
FROM emp
#課堂練習27
SELECT COUNT(job), COUNT(DISTINCT job)
FROM emp
#課堂練習26
SELECT SUM(sal),AVG(sal)
FROM emp
WHERE deptno=20;
#除COUNT(*)之外,其它所有分組函數都會忽略列中的空值,然後再進行運算; IFNULL==空值處理函數
SELECT AVG(comm),AVG(IFNULL(comm,0))
FROM emp;
#空值處理函數 IFNULL(comm,0)
SELECT ename 員工姓名,sal 工資收入,comm 獎金收入,sal+IFNULL(comm,0) 總收入
FROM emp;
SELECT COUNT(deptno),COUNT(DISTINCT deptno)
FROM emp
#五個組函數--聚合函數
SELECT MIN(hiredate),MAX(hiredate),MIN(sal),MAX(sal),AVG(sal),SUM(sal),COUNT(*)
FROM emp;
#課堂練習25
SELECT e.empno,e.ename,e.`job`,d.deptno,d.`loc`
FROM emp e,dept d
WHERE e.deptno = d.deptno AND (d.`loc`='CHICAGO' OR job='MANAGER');
#聯合查詢 UNION 去除重覆數據
SELECT e.empno,e.ename,d.deptno,d.dname
FROM emp e LEFT OUTER JOIN dept d ON(e.deptno = d.deptno)
UNION
SELECT e.empno,e.ename,d.deptno,d.dname
FROM emp e RIGHT OUTER JOIN dept d ON(e.deptno = d.deptno);
#聯合查詢 UNION ALL 保留重覆數據
SELECT e.empno,e.ename,d.deptno,d.dname
FROM emp e LEFT OUTER JOIN dept d ON(e.deptno = d.deptno)
UNION ALL
SELECT e.empno,e.ename,d.deptno,d.dname
FROM emp e RIGHT OUTER JOIN dept d ON(e.deptno = d.deptno);
#課堂練習24 右外連接 題目? 沒有下屬的也要查出來
SELECT e.`ename` ,e2.`ename`
FROM emp e RIGHT OUTER JOIN emp e2 ON(e.`mgr`=e2.`empno`);
#課堂練習23 左外連接 沒有領導的也要查出來
SELECT e.`ename` ,e2.`ename`
FROM emp e LEFT OUTER JOIN emp e2 ON(e.`mgr`=e2.`empno`);
#課堂練習22
SELECT ename,dname,hiredate
FROM emp e,dept d
WHERE e.`deptno`=d.`deptno` AND e.`hiredate` > '1980-5-1'
#課堂練習21 笛卡爾積數據
SELECT ename,dname
FROM emp,dept
#右外連接
SELECT e.`ename` ,e.`deptno`,d.`loc`
FROM emp e RIGHT OUTER JOIN dept d ON(e.`deptno`=d.`deptno`);
#左外連接
SELECT e.`ename` ,e.`deptno`,d.`loc`
FROM emp e LEFT OUTER JOIN dept d ON(e.`deptno`=d.`deptno`);
#等值連接 自然連接
SELECT e.`ename` ,e.`deptno`,d.`loc`
FROM emp e,dept d
WHERE e.`deptno`=d.`deptno`
#課堂練習20 自連接
SELECT e.`ename` 員工姓名,e.`empno` 員工編號,e2.`ename` 經理姓名,e2.`empno` 經理編號
FROM emp e,emp e2,dept d
WHERE e.`mgr`=e2.`empno` AND e.`deptno`=d.`deptno` AND (d.`loc`='NEW YORK' OR d.`loc`='CHICAGO');
#自連接
SELECT e.`ename` 員工姓名,e2.`ename` 上級姓名
FROM emp e,emp e2
WHERE e.`mgr`=e2.`empno`
#課堂練習19
SELECT e.`empno`,e.`ename`,e.`sal`,s.`grade`,d.`loc`
FROM emp e,dept d,salgrade s
WHERE e.`deptno`=d.`deptno` AND e.`sal` BETWEEN s.`losal` AND s.`hisal`
ORDER BY s.`grade` DESC;
#多於兩個表的連接
SELECT e.`ename`,e.`sal`,d.`dname`,s.`grade`
FROM emp e,dept d,salgrade s
WHERE e.`deptno`=d.`deptno` AND e.`sal` BETWEEN s.`losal` AND s.`hisal`;
#非等值連接
SELECT ename,sal,grade
FROM emp e,salgrade s
WHERE e.`sal` BETWEEN s.`losal` AND s.`hisal`;
#綜合練習4
SELECT ename,e.deptno,dname,sal
FROM emp e,dept d
WHERE e.`deptno`=d.`deptno` AND dname = 'RESEARCH' AND sal < 1500;
#綜合練習3
SELECT ename,loc
FROM emp e,dept d
WHERE e.`deptno`=d.`deptno` AND ename LIKE '%A%';
#綜合練習2
SELECT ename,comm,loc
FROM emp e,dept d
WHERE e.`deptno`=d.`deptno` AND loc='CHICAGO' AND comm IS NOT NULL;
#綜合練習1
SELECT ename,e.deptno,dname
FROM emp e,dept d
WHERE e.`deptno`=d.`deptno`
#多表等值連接查詢
SELECT empno,ename,dept.deptno,dname
FROM emp,dept
WHERE emp.`deptno`=dept.`deptno`
#使用表的別名
SELECT empno,ename,d.deptno,dname
FROM emp e,dept d
WHERE e.`deptno`=d.`deptno`
#課堂練習18
SELECT ename,hiredate,job
FROM emp
WHERE hiredate BETWEEN '1982-1-1' AND '1983-12-31' AND (job LIKE 'SALES%' OR job LIKE 'MAN%')
ORDER BY hiredate DESC
#課堂練習17
SELECT ename,deptno,sal
FROM emp
WHERE deptno NOT IN (10) AND sal BETWEEN 2000 AND 3000
ORDER BY deptno ASC,sal DESC
#課堂練習16
SELECT ename,deptno,sal
FROM emp
WHERE deptno IN (20,30)
ORDER BY sal
SELECT ename,deptno
FROM emp
WHERE deptno IN (20,30)
ORDER BY sal
#同時按多列排序
SELECT ename,deptno,sal
FROM emp
ORDER BY deptno ASC,sal DESC;
#DESC 降序 從大到小 == 空值小
SELECT ename,job,deptno,hiredate
FROM emp
ORDER BY hiredate DESC
#按三種方式排序
SELECT ename,job,deptno,hiredate
FROM emp
ORDER BY hiredate
SELECT ename,job,deptno,hiredate 入職日期
FROM emp
ORDER BY 入職日期
SELECT ename,job,deptno,hiredate 入職日期
FROM emp
ORDER BY 4
可以按照3種方式進行排序:分別是按列名排序、按列別名排序、按列序號排序。
ASC表示按升序排序(預設值), DESC表示按降序排序。
可以同時按照多個列名進行排序
空值在升序排列中排在最前面,在降序排列中排在最後 == 空值小
4種特殊比較運算符 BETWEEN..AND.. , IN, LIKE, IS NULL
#課堂練習15
SELECT ename,job,deptno
FROM emp
WHERE job IN ('SALESMAN','MANAGER') AND deptno IN (10,20) AND ename LIKE '%A%';
SELECT ename,job,deptno
FROM emp
WHERE (job ='SALESMAN' OR job='MANAGER') AND (deptno=10 OR deptno=20) AND ename LIKE '%A%';
#課堂練習14
SELECT ename,hiredate,job
FROM emp
WHERE hiredate BETWEEN '1981-1-1' AND '1981-12-31' AND job NOT LIKE 'SALES%';
SELECT ename,hiredate,job
FROM emp
WHERE hiredate >= '1981-1-1' AND hiredate <='1981-12-31' AND job NOT LIKE 'SALES%';
#課堂練習13 寫法1 使用特殊比較運算符
SELECT ename,deptno,sal
FROM emp
WHERE deptno IN (10,20) AND sal BETWEEN 3000 AND 5000;
#寫法2 使用邏輯運算符
SELECT ename,deptno,sal
FROM emp
WHERE (deptno = 10 OR deptno=20) AND (sal >= 3000 AND sal <= 5000);
#課堂練習12
SELECT ename,job,sal
FROM emp
WHERE sal > 2000 AND (job='MANAGER' OR job='SALESMAN');
#運算符的優先順序
SELECT ename, job, sal
FROM emp
WHERE ( job='SALESMAN'
OR job='PRESIDENT')
AND sal>1500;
SELECT ename, job, sal
FROM emp
WHERE job='SALESMAN'
OR job='PRESIDENT'
AND sal>1500;
SELECT ename,comm
FROM emp
WHERE comm IS NOT NULL;
SELECT ename,sal
FROM emp
WHERE sal NOT BETWEEN 3000 AND 5000;
NOT BETWEEN .. AND .. :不在某個區間
NOT IN (集合):不在某個集合內
NOT LIKE :不像.....
IS NOT NULL: 不是空
#課堂練習11
SELECT ename,comm
FROM emp
WHERE comm IS NULL;
#課堂練習10
SELECT *
FROM emp
WHERE ename LIKE '%T_';
#課堂練習9
SELECT *
FROM emp
WHERE ename LIKE 'W%';
# IS NULL
SELECT ename,mgr
FROM emp
WHERE mgr IS NULL;
SELECT ename,comm
FROM emp
WHERE comm IS NULL;
#Like運算符
SELECT ename
FROM emp
WHERE ename LIKE 'S%';
SELECT ename
FROM emp
WHERE ename LIKE 'S_';
SELECT ename
FROM emp
WHERE ename LIKE '%A%';
SELECT ename
FROM emp
WHERE ename LIKE '__A%';
#課堂練習8
SELECT ename,sal
FROM emp
WHERE sal BETWEEN 3000 AND 5000;
#課堂練習7
SELECT ename,hiredate
FROM emp
WHERE hiredate BETWEEN '1982-1-1' AND '1985-12-31';
#IN運算符
select empno,ename,deptno
from emp
where deptno in (10,20);
#使用BETWEEN .. AND.. 可以查詢出某列的值在某個範圍內(包括邊界值)的數據行
SELECT empno,ename,sal
FROM emp
WHERE sal BETWEEN 1250 AND 1600;
#課堂練習6
SELECT empno,ename,deptno
FROM emp
WHERE deptno <> 10;
SELECT empno,ename,deptno
FROM emp
WHERE deptno != 10;
#課堂練習5
SELECT empno,ename,hiredate
FROM emp
WHERE hiredate < '1985-12-31';
#課堂練習4
SELECT empno,ename,job
FROM emp
WHERE job='SALESMAN';
#帶條件查詢2 非等值情況
SELECT empno,ename,hiredate
FROM emp
WHERE hiredate > '1985-01-01';
#帶條件查詢2
SELECT empno,ename,job
FROM emp
WHERE job='CLERK';
SELECT empno,ename,job
FROM emp
WHERE job="CLERK";
#帶條件查詢1
SELECT empno,ename,deptno
FROM emp
WHERE deptno=20;
#課堂練習3
SELECT DISTINCT job FROM emp
#排除重覆數據的查詢
SELECT DISTINCT deptno FROM emp
#課堂練習2 ==?
SELECT ename 員工姓名,sal 工資收入,comm 獎金收入,sal+comm 總收入 FROM emp;
#課堂練習1
SELECT ename,sal 漲薪前,sal*1.2 漲薪後 FROM emp;
#空值參與算術運算,運算後的結果仍為NULL ==待解決?
SELECT ename,sal,comm,sal+comm 月總收入 FROM emp
#數值類型的欄位可以做數學運算
SELECT empno,ename,sal,sal*12 年薪 FROM emp
#按欄位別名查詢 單雙引號的使用
SELECT empno 編號,ename "姓 名",sal '工 資',comm "獎,金" FROM emp
#按欄位別名查詢
SELECT empno 編號,ename 姓名,sal 工資,comm 獎金 FROM emp
#查詢指定欄位
SELECT empno,ename,sal,comm FROM emp
#查詢所有欄位
SELECT * FROM emp