MySQL基础篇(day03,复习自用)

news/2024/11/24 8:30:53/

MySQL第三天

  • 排序与分页
    • 内容
    • 练习
  • 多表查询
    • 内容
    • 练习

排序与分页

内容

在这里插入图片描述

#第五章 排序与分页#1.排序
#如果没有使用排序操作,默认情况下查询返回的数据是按照添加数据的顺序显示的。
SELECT * FROM employees;#1.1基本使用
#使用 ORDER BY 对查询到的数据进行排序操作
#升序:ASC (ascend)
#降序:DESC (descend)#练习:按照salary从高到低的顺序显示员工信息SELECT employee_id,last_name,salary
FROM employees
ORDER BY salary DESC;#练习:按照salary从低到高的顺序显示员工信息SELECT employee_id,last_name,salary
FROM employees
ORDER BY salary ;#如果在ORDER BY 之后没有显式指名排序的方式的话,则默认按照升序排序。#2.我们可以使用列的别名,进行排序
SELECT employee_id,salary,salary*12 annual_sal
FROM employees
ORDER BY annual_sal;#列的别名只能在ORDER BY中使用,不能在WHERE中使用
SELECT employee_id,salary,salary * 12 annual_sal
FROM employees
WHERE annual_sal >81600;#会报错#3.强调格式:WHERE需要声明在FROM后,ORDER BY之前
SELECT employee_id,salary
FROM employees
WHERE department_id IN (50,60,70)
ORDER BY department_id DESC;#4.二级排序#练习:显示员工信息,按照department_id的降序排列,salary的升序排列
SELECT employee_id,salary,department_id
FROM employees
ORDER BY department_id DESC,salary ASC;#2.分页
#2.1mysql使用limit实现数据的分页显示
#需求1:每页显示20条记录,此时显示第一页
SELECT employee_id,last_name
FROM employees
LIMIT 0,20;#需求2:每页显示20条记录,显示第2页
SELECT employee_id,last_name
FROM employees
LIMIT 20,20;#需求3:每页显示20条记录,显示第3页
SELECT employee_id,last_name
FROM employees
LIMIT 40,20;#需求:每页显示pageSize条记录,此时显示配置第PageNo条
#公式:LIMIT (pageNo - 1) * pageSize#2.2 WHERE ...ORDER BY... LIMIT声明顺序
#LIMIT 的格式:严格来说:LIMIT位置偏移量,条目数
#"LIMIT 0,条目数"等价于"LIMIT 条目数"
SELECT employee_id,last_name,salary
FROM employees 
WHERE salary > 6000
ORDER BY salary DESC
#LIMIT 0,10;
LIMIT 10;#练习:表里有107条数据,我们只想要显示第32、33条数据
SELECT employee_id,last_name
FROM employees
LIMIT 31,2;#下标从0开始#2.3	MySQL8.0新特性:LiMIT...OFFSET...
#练习:表里有107条数据,我们只想要显示第32、33条数据
SELECT employee_id,last_name
FROM employees
LIMIT 2 OFFSET 31;#练习:查询员工表中工资最高的员工信息
SELECT employee_id,last_name,salary
FROM employees
ORDER BY salary DESC
#limit 0,1;
LIMIT 1;#2,4 LIMIT 可以使用在MySQL,PGSQL,MariaDB,SQLite等数据库中使用,表示分页
# 不能使用在SQL Server、DB2、Oracle!

练习

#1. 查询员工的姓名和部门号和年薪,按年薪降序,按姓名升序显示
SELECT last_name,department_id, salary * 12 annuan_sal
FROM employees
ORDER BY annuan_sal DESC,last_name ASC;#2. 选择工资不在 8000 到 17000 的员工的姓名和工资,按工资降序,显示第21到40位置的数据
SELECT last_name,salary
FROM employees
WHERE salary NOT BETWEEN 8000 AND 17000
ORDER BY salary DESC
LIMIT 20,20;#3. 查询邮箱中包含 e 的员工信息,并先按邮箱的字节数降序,再按部门号升序
SELECT * FROM employees
#where email like "%e%"
WHERE email REGEXP '[e]'
ORDER BY LENGTH(email) DESC,department_id ASC;

多表查询

内容

在这里插入图片描述

#多表查询
#1.熟系常见的几个表
DESC employees;
DESC departments;
DESC locations;
#查询员工名为'Abel'的人在哪个城市工作
SELECT *
FROM employees
WHERE last_name = 'Abel';SELECT *
FROM departments
WHERE department_id = 80;SELECT *
FROM locations
WHERE location_id = 2500;#2.出现笛卡尔积的错误
#错误的原因:缺少了多表的链接条件
#错误的实现方式:每个员工都与每个部门匹配了一遍。
SELECT employee_id,department_name
FROM employees,departments; #查询出2889条记录#错误的方式
SELECT employee_id,department_name
FROM employees CROSS JOIN departments;SELECT *
FROM employees; #107条记录SELECT 2889/107
FROM DUAL;#27SELECT *
FROM departments;#27条记录#3.多表查询的正确方式:需要有连接条件
SELECT employee_id,department_name
FROM employees,departments
#两个表的连接条件
WHERE employees.department_id = departments.department_id;#如果查询语句中出现了多个表中都存在的字段,则必须指明此字段所在的表
SELECT employees.employee_id,departments.department_name,employees.department_id
FROM employees,departments
WHERE employees.department_id = departments.department_id;#建议:从sql优化的角度,建议多表查询时,每个字段前都指明其所在的表#5.可以给表起别名,在select和where中使用表的别名。
SELECT emp.employee_id,dept.department_name,emp.department_id
FROM employees emp,departments dept
WHERE emp.department_id = dept.department_id;#如果给表起了别名,一旦在select或where中使用表名的话,则必须使用表的别名,而不能再使用表的原名。
#如下的错误
SELECT emp.employee_id,dept.department_name,emp.department_id
FROM employees emp,departments dept#会报错
WHERE emp.department_id = departments.department_id;#6.如果有n个表实现多表的查询,则需要至少n-1个连接条件
#练习:查询员工的employee_id,last_name,department_name,city
SELECT e.employee_id,e.last_name,d.department_name,l.city,e.department_id,l.location_id
FROM employees e,departments d,locations l
WHERE e.department_id = d.department_id
AND d.location_id = l.location_id;#7.多表查询的分类
/*角度1:等值连接 vs 非等值连接角度2:自连接 vs 非自连接角度3:内连接 vs 外连接*/#7.1等值连接 vs 非等值连接
#非等值连接的例子:
SELECT *
FROM job_grades;#对应工资范围的等级SELECT last_name,salary,grade_level
FROM employees e, job_grades j
#where e.salary between j.lowest_sal and j.highest_sal;
WHERE e.salary >= j.lowest_sal AND e.salary <= j.highest_sal;#7.2 自连接 vs 非自连接
SELECT * FROM employees;#自连接的例子
#练习:查询员工id,员工姓名及其管理者的id和姓名
SELECT emp.employee_id,emp.last_name,mgr.employee_id,mgr.last_name
FROM employees emp,employees mgr
WHERE emp.manager_id = mgr.employee_id;#7.3内连接 vs 外连接
#内连接:合并具有同一列的两个以上的表的行,结果集中不包含一个表与另一个表不匹配的行
SELECT employee_id,department_name
FROM employees e,departments d
#只有106条记录
WHERE e.department_id = d.department_id;#外连接:合并具有同一列的两个以上的表的行,结果集中除了包含一个表与另一个表不匹配的行
#	还查询到了左表或右表中不匹配的行#外连接的分类:左外连接、右外连接、满外连接#左外连接:两个表在连接过程中除了返回满足连接条件的行以外还返回左表中不满足条件的行,这种连接称为左外连接
#右外连接:两个表在连接过程中除了返回满足连接条件的行以外还返回右表中不满足条件的行,这种连接称为右外连接#练习:查询所有的员工的last_name,department_name信息
SELECT employee_id,department_name
FROM employees e,departments d
WHERE e.department_id = d.department_id;#需要使用左外连接#SQL92语法实现内连接:见上,略
#SQL92语法实现外连接:使用 + -------MySQL不支持SQL92语法中外连接的写法!
SELECT employee_id,department_name
FROM employees e,departments d
WHERE e.department_id = d.department_id(+);#会报错#SQL99语法中使用JOIN...ON的方式实现多表的查询。这种方式也能解决外连接的问题,MySQL是支持这种方式的
#SQL99语法如何实现多表的查询#SQL99语法实现内连接;
SELECT last_name,department_name
FROM employees e INNER JOIN departments d
ON e.department_id = d.department_id;#inner可以省略SELECT last_name,department_name,city
FROM employees e 
JOIN departments d
ON e.department_id = d.department_id
JOIN locations l
ON d.location_id = l.location_id;#SQL99语法实现外连接
#练习:查询所有的员工的last_name,department_name信息
#左外连接:
SELECT last_name,department_name
FROM employees e LEFT OUTER JOIN departments d#OUTER可以省略
ON e.department_id = d.department_id;#右外连接:
SELECT last_name,department_name
FROM employees e RIGHT OUTER JOIN departments d#OUTER可以省略
ON e.department_id = d.department_id;#满外连接:mysql 不支持 FULL OUTER JOIN
SELECT last_name,department_name
FROM employees e FULL OUTER JOIN departments d#OUTER可以省略
ON e.department_id = d.department_id;#8.UNION和UNION ALL的使用
#UNION:会执行去重操作
#UNION ALL:不会执行去重操作
#结论:如果明确知道合并数据后的结果数据不存在重复数据,或者不需要去除重复的数据
#则尽量使用 UNION ALL语句,以提高数据查询的效率。#9.7中JOIN的实现:#内连接 
SELECT employee_id,department_name
FROM employees e JOIN departments d
ON e.department_id = d.department_id;#左上图:左外连接 
SELECT employee_id,department_name
FROM employees e LEFT JOIN departments d
ON e.department_id = d.department_id;#右上图:右外连接 
SELECT employee_id,department_name
FROM employees e RIGHT JOIN departments d
ON e.department_id = d.department_id;#左中图:
SELECT employee_id,department_name
FROM employees e LEFT JOIN departments d
ON e.department_id = d.department_id
WHERE d.department_id IS NULL;#右中图:
SELECT employee_id,department_name
FROM employees e RIGHT JOIN departments d
ON e.department_id = d.department_id
WHERE e.department_id IS NULL;#左下图:满外连接 
#方式1:左上图 UNION ALL  右中图 
SELECT employee_id,department_name
FROM employees e LEFT JOIN departments d
ON e.department_id = d.department_id
UNION ALL
SELECT employee_id,department_name
FROM employees e RIGHT JOIN departments d
ON e.department_id = d.department_id
WHERE e.department_id IS NULL;#方式2:左中图 UNION ALL 右上图
SELECT employee_id,department_name
FROM employees e RIGHT JOIN departments d
ON e.department_id = d.department_id
WHERE e.department_id IS NULL
UNION ALL
SELECT employee_id,department_name
FROM employees e RIGHT JOIN departments d
ON e.department_id = d.department_id;#右下图:左中图 UNION ALL 右中图
SELECT employee_id,department_name
FROM employees e LEFT JOIN departments d
ON e.department_id = d.department_id
WHERE d.department_id IS NULL
UNION ALL
SELECT employee_id,department_name
FROM employees e RIGHT JOIN departments d
ON e.department_id = d.department_id
WHERE e.department_id IS NULL;#10.SQL99语法的新特性1:自然连接
SELECT employee_id,last_name,department_name
FROM employees e JOIN departments d
ON e.department_id = d.department_id
AND e.manager_id = d.manager_id;#NATURAL JOIN:它会帮你自动查询两张连接表中的所有相同的字段,然后进行等值连接
SELECT employee_id,last_name,department_name
FROM employees NATURAL JOIN departments;#11.SQL99语法的新特性2:USING
SELECT employee_id,last_name,department_name
FROM employees e JOIN departments d
ON e.department_id = d.department_id;SELECT employee_id,last_name,department_name
FROM employees e JOIN departments d
USING (department_id);#拓展:
SELECT last_name,job_title,department_name
FROM employees INNER JOIN departments INNER JOIN jobs
ON employees.department_id = departments.department_id
AND employees.job_id = jobs.job_id;

练习

SELECT *
FROM employees, departments;# 1.显示所有员工的姓名,部门号和部门名称。
SELECT last_name,e.department_id,department_name
FROM employees e LEFT JOIN departments d #所有员工,外连接
ON e.department_id = d.department_id;SELECT * FROM employees;
SELECT * FROM departments;
# 2.查询90号部门员工的job_id和90号部门的location_id
SELECT job_id,location_id
FROM employees e JOIN departments d
#on e.department_id = 90;#错误
ON e.department_id = d.department_id #不能少了连接条件 
WHERE d.department_id = 90;# 3.选择所有有奖金的员工的 last_name , department_name , location_id , citySELECT e.last_name,commission_pct,d.department_name,l.location_id,city
FROM employees e LEFT JOIN departments d
ON e.department_id = d.department_id
LEFT JOIN  locations l
ON d.location_id = l.location_id
WHERE e.commission_pct IS NOT NULL;#添加一句WHERE,也应该是35条记录SELECT * FROM employees
WHERE commission_pct IS NOT NULL;#35条# 4.选择city在Toronto工作的员工的 last_name , job_id , department_id , department_name
SELECT last_name,job_id,e.department_id,department_name
FROM employees e JOIN departments d
ON e.department_id = d.department_id
JOIN  locations l
ON d.location_id = l.location_id #切记:不要忘了关联条件
WHERE l.city = 'Toronto';#切记,不要少了where#SQL92的语法
SELECT last_name,job_id,e.department_id,department_name
FROM employees e,departments d,locations l
WHERE e.department_id = d.department_id
AND d.location_id = l.location_id
AND l.city = 'Toronto';SELECT * FROM departments;
SELECT * FROM locations;
SELECT * FROM employees;
# 5.查询员工所在的部门名称、部门地址、姓名、工作、工资,其中员工所在部门的部门名称为’Executive’
SELECT department_name,street_address,last_name,job_id,salary
FROM employees e JOIN departments d 
ON e.department_id = d.department_id
JOIN  locations l
ON d.location_id = l.location_id
AND d.department_name = 'Executive';
#where d.department_name = 'Executive';SELECT * FROM employees;
# 6.选择指定员工的姓名,员工号,以及他的管理者的姓名和员工号,结果类似于下面的格式
employees Emp# manager Mgr#
kochhar 101 king 100SELECT emp.last_name "employees",emp.employee_id "Emp#",mgr.last_name "manager",mgr.employee_id "Mgr#"
FROM employees emp LEFT JOIN employees mgr
ON emp.manager_id = mgr.employee_id;#不会写的题目:7,8,9
# 7.查询哪些部门没有员工
SELECT d.department_id
FROM departments d LEFT JOIN employees e
ON d.department_id = e.department_id
WHERE e.department_id IS NULL;#本题也可以使用子查询:暂时不讲# 8. 查询哪个城市没有部门
SELECT l.location_id,l.city
FROM locations l LEFT JOIN departments d
ON l.location_id = d.location_id
WHERE d.location_id IS NULL;# 9. 查询部门名为 Sales 或 IT 的员工信息
SELECT e.employee_id,e.last_name,d.department_id
FROM employees e JOIN departments d 
ON  e.department_id = d.department_id
WHERE d.department_name IN ('Sales','IT');

http://www.ppmy.cn/news/615566.html

相关文章

HarmonyOS学习路之开发篇—AI功能开发(助手类意图识别)

助手类意图识别概述 随着人机交互越来越普遍&#xff0c;设备需要理解用户下达的各种指令&#xff0c;方便用户的操作。助手类意图识别能够利用机器学习技术&#xff0c;对用户发送给设备的文本消息进行语义分析和意图识别&#xff0c;进而衍生出各种智能的应用场景&#xff0…

(附源码)ssm户外用品商城 毕业设计 112346

基于ssm户外用品商城 摘 要 近年来&#xff0c;随着移动互联网的快速发展&#xff0c;电子商务越来越受到网民们的欢迎&#xff0c;电子商务对国家经济的发展也起着越来越重要的作用。简单的流程、便捷可靠的支付方式、快捷畅通的物流快递、安全的信息保护都使得电子商务越来越…

使用fastJson踩坑记录汇总

坑1-属性方法命名问题 问题表象 在使用fastjson工具把一个领域DTO对象转为 JSON 字符串&#xff0c;结果导致了非属性的 get 方法被调用了&#xff0c;此时延时加载所需的 bean 还没有注入&#xff0c;导致出现空指针。 场景复现 先构建一个一个学生类&#xff0c;里面只有…

举例说明什么是卷积计算

卷积计算是一种数学运算&#xff0c;主要用于信号处理、图像处理等领域。它涉及到两个函数&#xff08;通常称为信号和核&#xff09;的互相关运算。在图像处理中&#xff0c;卷积计算通常用于实现图像的平滑、锐化、边缘检测等操作。 以图像处理为例&#xff0c;我们可以用一个…

CSS 苹方字体

转自&#xff1a;https://www.jianshu.com/p/bc9f57a645bd

自然语言处理(概念)

1、 RNN模型简介 1.2传统RNN模型 2、LSTM模型 3、GRU模型 5、注意力机制 6、人名分类器 7 、BERT 8、Transformer 的结构是什么样子的&#xff1f; 各个子模块有什么作用&#xff1f; 8.1 Encoder模块 8.2 Decoder模块 8.3 Transformer 结构中的Decoder端具体输入是什么&#…

CSS滚动艺术字体

CSS布局 全局&#xff1a; * {margin: 0;padding: 0;} 字体调整&#xff1a; h1 {font-size: 18px;text-align: center;margin-top: 30%;background-image: -webkit-linear-gradient(left, blue,pink 10%, #65c0e0 20%, skyblue 30%,blue 40%, MediumVioletRed 50%, Purple…

Cocos Store 80款方正字体对味开发者:2元“静蕾体”受追捧

触控Cocos Store精准解读开发者群体所需&#xff0c;贴心打造“游戏开发工具素材军火库”&#xff0c;除已上架12万海量素材&#xff0c;还展开了与方正字体的战略合作&#xff0c;掀开字体2元时代帷幕。值得一提的是&#xff0c;首批上架的80款方正非商业版字体中&#xff0c;…