翼度科技»论坛 编程开发 mysql 查看内容

2023_10_10_MYSQL_DAY_02_笔记

6

主题

6

帖子

18

积分

新手上路

Rank: 1

积分
18
2023_10_10_MYSQL_DAY_02_笔记
  1. #在 FROM 子句中使用子查询
  2. SELECT  a.ename, a.sal, a.deptno, b.salavg
  3. FROM    emp a, (SELECT   deptno, AVG(sal) salavg
  4.                 FROM     emp
  5.                 GROUP BY deptno) b
  6. WHERE   a.deptno = b.deptno  AND    a.sal > b.salavg;
  7. #子查询里的空值处理
  8. SELECT    ename
  9. FROM     emp
  10. WHERE  empno NOT IN (SELECT IFNULL (mgr,0) FROM   emp);
  11. #多列子查询
  12. SELECT deptno,ename,hiredate
  13. FROM emp
  14. WHERE (deptno,hiredate) IN (SELECT deptno ,MIN(hiredate)
  15.                             FROM emp
  16.                             GROUP BY deptno);
  17. #课堂练习41
  18. SELECT ename,job
  19. FROM emp
  20. WHERE job = ANY (SELECT job FROM emp WHERE deptno=10)
  21.       AND deptno <> 10;
  22.           
  23.           
  24. #课堂练习40
  25. SELECT ename,hiredate
  26. FROM emp
  27. WHERE hiredate > ALL (SELECT hiredate FROM emp WHERE deptno=10)
  28.       AND deptno <> 10;
  29. #课堂练习39
  30. SELECT ename,hiredate
  31. FROM emp
  32. WHERE hiredate > ANY (SELECT hiredate FROM emp WHERE deptno=10)
  33.       AND deptno <> 10;
  34. #多行子查询 all操作符
  35. SELECT empno, ename, job, sal
  36. FROM     emp
  37. WHERE     sal > ALL (SELECT sal FROM emp WHERE  deptno= 20)
  38. AND    deptno <> 20;
  39. SELECT empno, ename, job, sal
  40. FROM     emp
  41. WHERE     sal < ALL (SELECT sal FROM emp WHERE  deptno= 20)
  42. AND    deptno <> 20;
  43. #多行子查询 any操作符
  44. SELECT ename,sal,empno
  45. FROM emp
  46. WHERE empno < ANY (SELECT mgr FROM emp ORDER BY mgr DESC);
  47. SELECT ename,sal,empno
  48. FROM emp
  49. WHERE empno > ANY (SELECT mgr FROM emp ORDER BY mgr DESC);
  50. SELECT ename,sal,empno
  51. FROM emp
  52. WHERE empno = ANY (SELECT mgr FROM emp ORDER BY mgr DESC);
  53. #多行子查询 in操作符
  54. SELECT ename,sal
  55. FROM emp
  56. WHERE empno IN (SELECT mgr FROM emp);
  57. #课堂练习38
  58. SELECT deptno,AVG(sal)
  59. FROM emp
  60. GROUP BY deptno
  61. HAVING AVG(sal) > (SELECT AVG(sal) FROM emp WHERE deptno=10);
  62. #课堂练习37
  63. SELECT ename,sal,dname
  64. FROM emp e,dept d
  65. WHERE e.`deptno`=d.`deptno`
  66.       AND sal > (SELECT sal FROM emp WHERE ename='SMITH')
  67.       AND d.`loc`='CHICAGO';
  68. #课堂练习36
  69. SELECT ename,hiredate
  70. FROM emp
  71. WHERE hiredate = (SELECT MIN(hiredate) FROM emp);
  72. # HAVING子句中使用子查询
  73. SELECT  deptno, MIN(sal)
  74. FROM    emp
  75. GROUP BY  deptno
  76. HAVING    MIN(sal) > (SELECT MIN(sal) FROM  emp WHERE deptno = 20);
  77. #子查询中使用组函数
  78. SELECT ename,job,sal
  79. FROM emp
  80. WHERE sal = (SELECT MIN(sal) FROM emp);
  81. #单行子查询语句
  82. SELECT ename,job
  83. FROM emp
  84. WHERE job = (SELECT job FROM emp WHERE empno=7369)
  85.      AND sal > (SELECT sal FROM emp WHERE empno=7876);
  86. #子查询 单行子查询
  87. SELECT ename
  88. FROM emp
  89. WHERE sal > (SELECT sal FROM emp WHERE ename='JONES');
  90. #课堂练习35
  91. SELECT ename,hiredate,deptno
  92. FROM emp
  93. LIMIT 0,5; #第1页
  94. SELECT ename,hiredate,deptno
  95. FROM emp
  96. LIMIT 5,5; #第2页
  97. SELECT ename,hiredate,deptno
  98. FROM emp
  99. LIMIT 10,5; #第3页
  100. #课堂练习34
  101. SELECT e.deptno,dname,COUNT(*),AVG(sal)
  102. FROM emp e,dept d
  103. WHERE e.`deptno`=d.`deptno`
  104. GROUP BY e.deptno,dname
  105. HAVING COUNT(*) > 2 AND AVG(sal) > 2000
  106. ORDER BY COUNT(*);
  107. #七个子句的查询
  108. SELECT job,SUM(sal)
  109. FROM emp
  110. WHERE job NOT LIKE 'SALES%'
  111. GROUP BY job
  112. HAVING SUM(sal) > 5000
  113. ORDER BY SUM(sal)
  114. LIMIT 0,2;
  115. #分页查询
  116. SELECT empno,ename,deptno,sal
  117. FROM emp
  118. ORDER BY empno
  119. LIMIT 5,5; #第2页
  120. SELECT empno,ename,deptno,sal
  121. FROM emp
  122. ORDER BY empno
  123. LIMIT 0,5; #第1页
  124. #课堂练习33
  125. SELECT e.deptno,dname,COUNT(*)
  126. FROM emp e,dept d
  127. WHERE e.`deptno`=d.`deptno`
  128. GROUP BY e.deptno,dname
  129. HAVING COUNT(*) > 2;
  130. 总结:
  131. 1、WHERE子句用来过滤分组之前的记录,不能使用组函数
  132. 2、HAVING子句用来过滤分组之后的记录,可以使用组函数
  133. #6个子句的查询
  134. SELECT job,SUM(sal)
  135. FROM emp
  136. WHERE job NOT LIKE 'SALES%'
  137. GROUP BY job
  138. HAVING SUM(sal) > 5000
  139. ORDER BY SUM(sal);
  140. 查询语句执行过程:
  141. 1、通过FROM子句中找到需要查询的表;
  142. 2、通过WHERE子句进行非分组函数筛选判断;
  143. 3、通过GROUP BY子句完成分组操作;
  144. 4、通过HAVING子句完成组函数筛选判断;
  145. 5、通过SELECT子句选择显示的列或表达式及组函数;
  146. 6、通过ORDER BY子句进行排序操作。
  147. #HAVING 子句
  148. SELECT deptno,COUNT(empno)
  149. FROM emp
  150. GROUP BY deptno
  151. HAVING COUNT(empno) > 3;
  152. SELECT deptno,MAX(sal)
  153. FROM emp
  154. GROUP BY deptno
  155. HAVING MAX(sal) > 2900;
  156. #课堂练习32
  157. SELECT COUNT(*),MAX(sal),MIN(sal),d.`loc`
  158. FROM emp e,dept d
  159. WHERE e.`deptno`=d.`deptno` AND d.`loc`='CHICAGO'
  160. GROUP BY d.`loc`;
  161. #课堂练习31 自连接
  162. SELECT e2.`empno` 经理编号,e2.`ename` 经理姓名,COUNT(*)
  163. FROM emp e,emp e2
  164. WHERE e.`mgr`=e2.`empno`
  165. GROUP BY e2.`empno`,e2.`ename`;
  166. #课堂练习30
  167. SELECT d.`deptno`,d.`dname`,e.`job`,COUNT(empno),MAX(sal),MIN(sal)
  168. FROM emp e,dept d
  169. WHERE e.`deptno`=d.`deptno`
  170. GROUP BY d.`deptno`,d.`dname`,e.`job`;
  171. #多表查询分组查询
  172. SELECT d.`deptno`,d.`dname`,COUNT(empno),MAX(sal)
  173. FROM emp e,dept d
  174. WHERE e.`deptno`=d.`deptno`
  175. GROUP BY d.`deptno`,d.`dname`;
  176. #按多列分组查询
  177. SELECT deptno,job,SUM(sal)
  178. FROM emp
  179. GROUP BY deptno,job;
  180. #分组子句
  181. SELECT job,COUNT(empno)
  182. FROM emp
  183. GROUP BY job;
  184. SELECT deptno,AVG(sal)
  185. FROM emp
  186. GROUP BY deptno;
  187. #课堂练习29
  188. SELECT COUNT(*),MAX(sal),MIN(sal)
  189. FROM emp
  190. WHERE deptno=30;
  191. #课堂练习28
  192. SELECT (sal+IFNULL(comm,0))*12 年收入
  193. FROM emp
  194. #课堂练习27
  195. SELECT COUNT(job), COUNT(DISTINCT job)
  196. FROM emp
  197. #课堂练习26
  198. SELECT SUM(sal),AVG(sal)
  199. FROM emp
  200. WHERE deptno=20;
  201. #除COUNT(*)之外,其它所有分组函数都会忽略列中的空值,然后再进行运算; IFNULL==空值处理函数
  202. SELECT AVG(comm),AVG(IFNULL(comm,0))
  203. FROM emp;
  204. #空值处理函数 IFNULL(comm,0)
  205. SELECT ename 员工姓名,sal 工资收入,comm 奖金收入,sal+IFNULL(comm,0) 总收入
  206. FROM emp;
  207. SELECT COUNT(deptno),COUNT(DISTINCT deptno)
  208. FROM emp
  209. #五个组函数--聚合函数
  210. SELECT MIN(hiredate),MAX(hiredate),MIN(sal),MAX(sal),AVG(sal),SUM(sal),COUNT(*)
  211. FROM emp;
  212. #课堂练习25
  213. SELECT e.empno,e.ename,e.`job`,d.deptno,d.`loc`
  214. FROM emp e,dept d
  215. WHERE e.deptno = d.deptno AND (d.`loc`='CHICAGO' OR job='MANAGER');
  216. #联合查询 UNION 去除重复数据
  217. SELECT e.empno,e.ename,d.deptno,d.dname
  218. FROM emp e LEFT OUTER JOIN dept d ON(e.deptno = d.deptno)
  219. UNION
  220. SELECT e.empno,e.ename,d.deptno,d.dname
  221. FROM emp e RIGHT OUTER JOIN dept d ON(e.deptno = d.deptno);
  222. #联合查询  UNION ALL 保留重复数据
  223. SELECT e.empno,e.ename,d.deptno,d.dname
  224. FROM emp e LEFT OUTER JOIN dept d ON(e.deptno = d.deptno)
  225. UNION ALL
  226. SELECT e.empno,e.ename,d.deptno,d.dname
  227. FROM emp e RIGHT OUTER JOIN dept d ON(e.deptno = d.deptno);
  228. #课堂练习24 右外连接  题目?   没有下属的也要查出来
  229. SELECT e.`ename` ,e2.`ename`
  230. FROM emp e RIGHT OUTER JOIN emp e2 ON(e.`mgr`=e2.`empno`);
  231. #课堂练习23 左外连接  没有领导的也要查出来
  232. SELECT e.`ename` ,e2.`ename`
  233. FROM emp e LEFT OUTER JOIN emp e2 ON(e.`mgr`=e2.`empno`);
  234. #课堂练习22
  235. SELECT ename,dname,hiredate
  236. FROM emp e,dept d
  237. WHERE e.`deptno`=d.`deptno` AND e.`hiredate` > '1980-5-1'
  238. #课堂练习21 笛卡尔积数据
  239. SELECT ename,dname
  240. FROM emp,dept
  241. #右外连接
  242. SELECT e.`ename` ,e.`deptno`,d.`loc`
  243. FROM emp e RIGHT OUTER JOIN dept d ON(e.`deptno`=d.`deptno`);
  244. #左外连接
  245. SELECT e.`ename` ,e.`deptno`,d.`loc`
  246. FROM emp e LEFT OUTER JOIN dept d ON(e.`deptno`=d.`deptno`);
  247. #等值连接 自然连接
  248. SELECT e.`ename` ,e.`deptno`,d.`loc`
  249. FROM emp e,dept d
  250. WHERE e.`deptno`=d.`deptno`
  251. #课堂练习20 自连接
  252. SELECT e.`ename` 员工姓名,e.`empno` 员工编号,e2.`ename` 经理姓名,e2.`empno` 经理编号
  253. FROM emp e,emp e2,dept d
  254. WHERE e.`mgr`=e2.`empno` AND e.`deptno`=d.`deptno` AND (d.`loc`='NEW YORK' OR  d.`loc`='CHICAGO');
  255. #自连接
  256. SELECT e.`ename` 员工姓名,e2.`ename` 上级姓名
  257. FROM emp e,emp e2
  258. WHERE e.`mgr`=e2.`empno`
  259. #课堂练习19
  260. SELECT e.`empno`,e.`ename`,e.`sal`,s.`grade`,d.`loc`
  261. FROM emp e,dept d,salgrade s
  262. WHERE e.`deptno`=d.`deptno` AND e.`sal` BETWEEN s.`losal` AND s.`hisal`
  263. ORDER BY s.`grade` DESC;
  264. #多于两个表的连接
  265. SELECT e.`ename`,e.`sal`,d.`dname`,s.`grade`
  266. FROM emp e,dept d,salgrade s
  267. WHERE e.`deptno`=d.`deptno` AND e.`sal` BETWEEN s.`losal` AND s.`hisal`;
  268. #非等值连接
  269. SELECT ename,sal,grade
  270. FROM emp e,salgrade s
  271. WHERE e.`sal` BETWEEN s.`losal` AND s.`hisal`;
  272. #综合练习4
  273. SELECT ename,e.deptno,dname,sal
  274. FROM emp e,dept d
  275. WHERE e.`deptno`=d.`deptno` AND dname = 'RESEARCH' AND sal < 1500;
  276. #综合练习3
  277. SELECT ename,loc
  278. FROM emp e,dept d
  279. WHERE e.`deptno`=d.`deptno` AND ename LIKE '%A%';
  280. #综合练习2
  281. SELECT ename,comm,loc
  282. FROM emp e,dept d
  283. WHERE e.`deptno`=d.`deptno` AND loc='CHICAGO' AND comm IS NOT NULL;
  284. #综合练习1
  285. SELECT ename,e.deptno,dname
  286. FROM emp e,dept d
  287. WHERE e.`deptno`=d.`deptno`
  288. #多表等值连接查询
  289. SELECT empno,ename,dept.deptno,dname
  290. FROM emp,dept
  291. WHERE emp.`deptno`=dept.`deptno`
  292. #使用表的别名
  293. SELECT empno,ename,d.deptno,dname
  294. FROM emp e,dept d
  295. WHERE e.`deptno`=d.`deptno`
  296. #课堂练习18
  297. SELECT ename,hiredate,job
  298. FROM emp
  299. WHERE hiredate BETWEEN '1982-1-1' AND '1983-12-31' AND (job LIKE 'SALES%' OR job LIKE 'MAN%')
  300. ORDER BY hiredate DESC
  301. #课堂练习17
  302. SELECT ename,deptno,sal
  303. FROM emp
  304. WHERE deptno NOT IN (10) AND sal BETWEEN 2000 AND 3000
  305. ORDER BY deptno ASC,sal DESC
  306. #课堂练习16
  307. SELECT ename,deptno,sal
  308. FROM emp
  309. WHERE deptno IN (20,30)
  310. ORDER BY sal
  311. SELECT ename,deptno
  312. FROM emp
  313. WHERE deptno IN (20,30)
  314. ORDER BY sal
  315. #同时按多列排序
  316. SELECT ename,deptno,sal
  317. FROM emp
  318. ORDER BY deptno ASC,sal DESC;
  319. #DESC 降序  从大到小  == 空值小
  320. SELECT ename,job,deptno,hiredate
  321. FROM emp
  322. ORDER BY hiredate DESC
  323. #按三种方式排序
  324. SELECT ename,job,deptno,hiredate
  325. FROM emp
  326. ORDER BY hiredate
  327. SELECT ename,job,deptno,hiredate 入职日期
  328. FROM emp
  329. ORDER BY 入职日期
  330. SELECT ename,job,deptno,hiredate 入职日期
  331. FROM emp
  332. ORDER BY 4
  333. 可以按照3种方式进行排序:分别是按列名排序、按列别名排序、按列序号排序。
  334. ASC表示按升序排序(默认值), DESC表示按降序排序。
  335. 可以同时按照多个列名进行排序
  336. 空值在升序排列中排在最前面,在降序排列中排在最后 == 空值小
  337. 4种特殊比较运算符        BETWEEN..AND.. ,  IN,  LIKE,  IS NULL
  338. #课堂练习15
  339. SELECT ename,job,deptno
  340. FROM emp
  341. WHERE job IN ('SALESMAN','MANAGER') AND deptno IN (10,20) AND ename LIKE '%A%';
  342. SELECT ename,job,deptno
  343. FROM emp
  344. WHERE (job ='SALESMAN' OR job='MANAGER') AND (deptno=10 OR deptno=20) AND ename LIKE '%A%';
  345. #课堂练习14
  346. SELECT ename,hiredate,job
  347. FROM emp
  348. WHERE hiredate BETWEEN '1981-1-1' AND '1981-12-31' AND job NOT LIKE 'SALES%';
  349. SELECT ename,hiredate,job
  350. FROM emp
  351. WHERE hiredate >= '1981-1-1' AND hiredate <='1981-12-31' AND job NOT LIKE 'SALES%';
  352. #课堂练习13 写法1 使用特殊比较运算符
  353. SELECT ename,deptno,sal
  354. FROM emp
  355. WHERE deptno IN (10,20) AND sal BETWEEN 3000 AND 5000;
  356. #写法2 使用逻辑运算符
  357. SELECT ename,deptno,sal
  358. FROM emp
  359. WHERE (deptno = 10 OR deptno=20) AND (sal >= 3000 AND sal <= 5000);
  360. #课堂练习12
  361. SELECT ename,job,sal
  362. FROM emp
  363. WHERE sal > 2000 AND (job='MANAGER' OR job='SALESMAN');
  364. #运算符的优先级
  365. SELECT ename, job, sal
  366. FROM   emp
  367. WHERE ( job='SALESMAN'  
  368. OR  job='PRESIDENT')
  369. AND  sal>1500;
  370. SELECT ename, job, sal
  371. FROM   emp
  372. WHERE  job='SALESMAN'  
  373. OR  job='PRESIDENT'
  374. AND  sal>1500;
  375. SELECT ename,comm
  376. FROM emp
  377. WHERE comm IS NOT NULL;
  378. SELECT ename,sal
  379. FROM emp
  380. WHERE sal NOT BETWEEN 3000 AND 5000;
  381. NOT BETWEEN .. AND .. :不在某个区间
  382. NOT IN (集合):不在某个集合内
  383. NOT LIKE    :不像.....
  384. IS NOT NULL:  不是空
  385. #课堂练习11
  386. SELECT ename,comm
  387. FROM emp
  388. WHERE comm IS NULL;
  389. #课堂练习10
  390. SELECT *
  391. FROM emp
  392. WHERE ename LIKE '%T_';
  393. #课堂练习9
  394. SELECT *
  395. FROM emp
  396. WHERE ename LIKE 'W%';
  397. # IS NULL
  398. SELECT ename,mgr
  399. FROM emp
  400. WHERE mgr IS NULL;
  401. SELECT ename,comm
  402. FROM emp
  403. WHERE comm IS NULL;
  404. #Like运算符
  405. SELECT ename
  406. FROM emp
  407. WHERE ename LIKE 'S%';
  408. SELECT ename
  409. FROM emp
  410. WHERE ename LIKE 'S_';
  411. SELECT ename
  412. FROM emp
  413. WHERE ename LIKE '%A%';
  414. SELECT ename
  415. FROM emp
  416. WHERE ename LIKE '__A%';
  417. #课堂练习8
  418. SELECT ename,sal
  419. FROM emp
  420. WHERE sal BETWEEN 3000 AND 5000;
  421. #课堂练习7
  422. SELECT ename,hiredate
  423. FROM emp
  424. WHERE hiredate BETWEEN '1982-1-1' AND '1985-12-31';
  425. #IN运算符
  426. select empno,ename,deptno
  427. from emp
  428. where deptno in (10,20);
  429. #使用BETWEEN .. AND.. 可以查询出某列的值在某个范围内(包括边界值)的数据行
  430. SELECT empno,ename,sal
  431. FROM emp
  432. WHERE sal BETWEEN 1250 AND 1600;
  433. #课堂练习6
  434. SELECT empno,ename,deptno
  435. FROM emp
  436. WHERE deptno <> 10;
  437. SELECT empno,ename,deptno
  438. FROM emp
  439. WHERE deptno != 10;
  440. #课堂练习5
  441. SELECT empno,ename,hiredate
  442. FROM emp
  443. WHERE hiredate < '1985-12-31';
  444. #课堂练习4
  445. SELECT empno,ename,job
  446. FROM emp
  447. WHERE job='SALESMAN';
  448. #带条件查询2 非等值情况
  449. SELECT empno,ename,hiredate
  450. FROM emp
  451. WHERE hiredate > '1985-01-01';
  452. #带条件查询2
  453. SELECT empno,ename,job
  454. FROM emp
  455. WHERE job='CLERK';
  456. SELECT empno,ename,job
  457. FROM emp
  458. WHERE job="CLERK";
  459. #带条件查询1
  460. SELECT empno,ename,deptno
  461. FROM emp
  462. WHERE deptno=20;
  463. #课堂练习3
  464. SELECT DISTINCT job FROM emp
  465. #排除重复数据的查询
  466. SELECT DISTINCT deptno FROM emp
  467. #课堂练习2  ==?
  468. SELECT ename 员工姓名,sal 工资收入,comm 奖金收入,sal+comm 总收入 FROM emp;
  469. #课堂练习1
  470. SELECT ename,sal 涨薪前,sal*1.2 涨薪后 FROM emp;
  471. #空值参与算术运算,运算后的结果仍为NULL ==待解决?
  472. SELECT ename,sal,comm,sal+comm 月总收入 FROM emp
  473. #数值类型的字段可以做数学运算
  474. SELECT empno,ename,sal,sal*12 年薪 FROM emp
  475. #按字段别名查询  单双引号的使用
  476. SELECT empno 编号,ename "姓 名",sal '工 资',comm "奖,金" FROM emp
  477. #按字段别名查询
  478. SELECT empno 编号,ename 姓名,sal 工资,comm 奖金 FROM emp
  479. #查询指定字段
  480. SELECT empno,ename,sal,comm FROM emp
  481. #查询所有字段
  482. SELECT * FROM emp
复制代码
来源:https://www.cnblogs.com/Qinyyds/p/17756058.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

上一篇: 数据库分区

下一篇: 定位SQL慢查询

举报 回复 使用道具