From 8c6b61f0c523d2d191c1652a5c888ddce9f8fbc1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E5=BE=B7=E5=BA=B7?= <1669327472@qq.com> Date: Sat, 25 Mar 2023 11:44:54 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=8D=E4=B9=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../20230322 \345\244\215\344\271\240.md" | 279 ++++++++++++++++++ 1 file changed, 279 insertions(+) create mode 100644 "13 \345\274\240\345\276\267\345\272\267/20230322 \345\244\215\344\271\240.md" diff --git "a/13 \345\274\240\345\276\267\345\272\267/20230322 \345\244\215\344\271\240.md" "b/13 \345\274\240\345\276\267\345\272\267/20230322 \345\244\215\344\271\240.md" new file mode 100644 index 0000000..6b79999 --- /dev/null +++ "b/13 \345\274\240\345\276\267\345\272\267/20230322 \345\244\215\344\271\240.md" @@ -0,0 +1,279 @@ +```sql +use shushu; +create table price( + id int primary key auto_increment, + price double +); +insert into price values(1,2.30); +insert into price values(2,3.50); +insert into price values(4,null); +insert into price values(3,5.5); + +create table fruit( + id int primary key auto_increment, + name varchar(20) not null, + price_id int, + foreign key(price_id) references price(id) +); +insert into fruit values(1,'苹果',1); +insert into fruit values(2,'橘子',2); +insert into fruit values(3,'香蕉',null); + +-- 3.多表查询 + -- 3.1.笛卡尔积 + -- 需求:查询水果和价格信息 +SELECT name,price.price from fruit JOIN price on fruit.id=price.id; + +-- 3.2.内连接 +-- 避免笛卡尔积问题怎么处理? + + + +-- 练习:查询苹果信息,显示苹果id和价格 +-- 分析:在水果表中可以查看苹果id 名字 ,在价格表中可以查看价格和价格id + +SELECT fruit.id,price.price from fruit JOIN price on fruit.id=price.id +WHERE `name`='苹果'; + +-- 3.3左外连接 +-- 练习:查询所有水果信息和对应价格信息 +-- fruit f 位于left左边,称为左表,左外连接以左表为主 +-- price p 位于left右边,称为右表 +-- f.* :获取fruit水果表中的所有数据 +-- p.price : 获取price价格表的字段price的值 + + +-- 练习:查询没有写价格的水果及价格(先查询所有水果的价格信息,再筛选。) + +SELECT name,price.price from fruit JOIN price on fruit.id=price.id +WHERE price_id is null; + + +-- 3.4右外连接 +-- 练习:使用右外连接查询所有价格对应的水果名称和价格信息, +-- right join 表示右连接 +-- price p 称为右表 右外连接会查询右表即price表的全部数据以及和左表fruit f的交集 + + +-- 练习:查询出没有对应水果的价格编号和价格,水果名 +SELECT * from fruit left JOIN price on fruit.id=price.id +WHERE price_id is null; + + +-- 练习:使用左外连接查询价格对应的水果,显示所有价格 + +SELECT * from fruit left JOIN price on fruit.id=price.id +; +``` + + + +```sql +use shushu; + CREATE TABLE dept ( + id INT PRIMARY KEY AUTO_INCREMENT, -- 部门编号 + NAME VARCHAR(20) -- 部门名称 + ); + INSERT INTO dept (NAME) VALUES ('开发部'),('市场部'),('财务部'); + CREATE TABLE emp ( + id INT PRIMARY KEY AUTO_INCREMENT, + NAME VARCHAR(10), + gender CHAR(1), -- 性别 + salary DOUBLE, -- 工资 + join_date DATE, -- 入职日期 + dept_id INT, -- 部门编号 + foreign key(dept_id) references dept(id) + ); + INSERT INTO emp(NAME,gender,salary,join_date,dept_id) VALUES('孙悟空','男',7200,'2013-02-24',1); + INSERT INTO emp(NAME,gender,salary,join_date,dept_id) VALUES('猪八戒','男',3600,'2010-12-02',2); + INSERT INTO emp(NAME,gender,salary,join_date,dept_id) VALUES('唐僧','男',9000,'2008-08-08',2); + INSERT INTO emp(NAME,gender,salary,join_date,dept_id) VALUES('白骨精','女',5000,'2015-10-07',3); + INSERT INTO emp(NAME,gender,salary,join_date,dept_id) VALUES('蜘蛛精','女',4500,'2011-03-14',1); + + -- 1.查询工资最高的员工是谁 + SELECT name FROM emp WHERE salary=(SELECT max(salary) from emp); + -- 1.1 在emp员工表中查询最高工资 -- 9000 单行单列 + SELECT max(salary) from emp; + -- 1.2在emp员工表中根据上述查询的最高工资查询员工信息 + SELECT * FROM emp WHERE salary=(SELECT max(salary) from emp); + -- 2.查询工资小于平均工资的员工有哪些? + SELECT name from emp WHERE salary<(SELECT avg(salary) FROM emp); + -- 2.1在emp员工表中查询平均工资 -- 单行单列 5860 + SELECT avg(salary) FROM emp; + -- 2.2在emp员工表中查询小于上述的平均工资查询员工信息 + SELECT * from emp WHERE salary<(SELECT avg(salary) FROM emp); + -- 3.查询工资大于5000的员工,来自于哪些部门,显示部门的名字 + SELECT dept.`NAME` from emp JOIN dept on emp.dept_id=dept.id + WHERE emp.salary>5000; + -- 3.1 在员工表emp中查询工资大于5000的员工部门编号 + SELECT dept.id from emp JOIN dept on emp.dept_id=dept.id + WHERE emp.salary>5000; + -- 1 2 单列多值 作为子查询的条件使用in + + -- 3.2在dept部门表中根据上述查询的部门编号查询部门名字 + SELECT b.id,dept.`NAME` from dept JOIN (SELECT dept.id from emp JOIN dept on emp.dept_id=dept.id + WHERE emp.salary>5000) as b on dept.id=b.id; + -- 4. 查询开发部与财务部所有的员工信息 + SELECT * from emp JOIN dept on emp.dept_id=dept.id + WHERE dept.`NAME` in ('开发部','财务部'); + -- 4.1 在dept表查询开发部和财务部的部门编号 + SELECT id FROM dept WHERE name in ('开发部','财务部'); + -- 1 3 多行单列 作为子查询的条件使用in + + -- 4.2在emp员工表中根据上述查询的部门编号查询员工信息 + SELECT * from emp WHERE id in (SELECT id FROM dept WHERE name in ('开发部','财务部')); + -- 5.查询出2011年以后入职的员工信息,包括部门名称 + + -- 5.1在emp表中查询2011年以后入职的员工信息 + + -- 多行多列,作为子查询一般使用as起别名作为临时表和其他表查询. + + -- 5.2 将上述查询的结果作为临时表和dept表关联查询最后查询员工信息和部门名称 + + + +``` + +```sql + create table teacher ( + id int(11) primary key auto_increment, -- 教师编号 + name varchar(20) not null unique -- 教师姓名 + ); + insert into teacher values(null,'关羽'); +insert into teacher values(null,'张飞'); +insert into teacher values(null,'赵云'); + create table student ( + id int(11) primary key auto_increment, -- 学生编号 + name varchar(20) NOT NULL unique, -- 学生姓名 + city varchar(40) NOT NULL, -- 学生城市 + age int -- 学生年龄 + ) ; + insert into student values(null,'小王','北京',20); +insert into student values(null,'小李','上海',18); +insert into student values(null,'小周','北京',22); +insert into student values(null,'小刘','北京',21); +insert into student values(null,'小张','上海',22); +insert into student values(null,'小赵','北京',17); +insert into student values(null,'小蒋','上海',23); +insert into student values(null,'小韩','北京',25); +insert into student values(null,'小魏','上海',18); +insert into student values(null,'小明','广州',20); + create table course( + id int(11) primary key auto_increment, -- 课编号 + name varchar(20) not null unique, -- 课程名称 + teacher_id int(11) not null, -- 教师编号 + foreign key(teacher_id) references teacher (id) + ); +insert into course values(null,'语文',1); +insert into course values(null,'数学',1); +insert into course values(null,'生物',2); +insert into course values(null,'化学',2); +insert into course values(null,'物理',2); +insert into course values(null,'英语',3); + create table studentcourse ( + student_id int NOT NULL, -- 学生编号 + course_id int NOT NULL, -- 课程编号 + score double NOT NULL, -- 考试成绩 + foreign key (student_id) references student (id), + foreign key (course_id) references course (id) + ); + insert into studentcourse values(1,1,80); +insert into studentcourse values(1,2,90); +insert into studentcourse values(1,3,85); +insert into studentcourse values(1,4,78); +insert into studentcourse values(2,2,53); +insert into studentcourse values(2,3,77); +insert into studentcourse values(2,5,80); +insert into studentcourse values(3,1,71); +insert into studentcourse values(3,2,70); +insert into studentcourse values(3,4,80); +insert into studentcourse values(3,5,65); +insert into studentcourse values(3,6,75); +insert into studentcourse values(4,2,90); +insert into studentcourse values(4,3,80); +insert into studentcourse values(4,4,70); +insert into studentcourse values(4,6,95); +insert into studentcourse values(5,1,60); +insert into studentcourse values(5,2,70); +insert into studentcourse values(5,5,80); +insert into studentcourse values(5,6,69); +insert into studentcourse values(6,1,76); +insert into studentcourse values(6,2,88); +insert into studentcourse values(6,3,87); +insert into studentcourse values(7,4,80); +insert into studentcourse values(8,2,71); +insert into studentcourse values(8,3,58); +insert into studentcourse values(8,5,68); +insert into studentcourse values(9,2,88); +insert into studentcourse values(10,1,77); +insert into studentcourse values(10,2,76); +insert into studentcourse values(10,3,80); +insert into studentcourse values(10,4,85); +insert into studentcourse values(10,5,83); + + +-- 1.查询获得最高分的学生信息。 + -- 1.1 在中间表中查询最高分-- 95 单行单列 + SELECT max(score) from studentcourse; + -- 1.2 在中间表中根据上述查询的最高分查询学生id + SELECT student_id from studentcourse WHERE score=(SELECT max(score) from studentcourse); + -- 1.3在学生表中根据上述查询的学生id查询学生信息 -- 一个班级最高分95,可以有多名学员,结果有可能是多行多列 + SELECT * from student JOIN (SELECT student_id from studentcourse WHERE score=(SELECT max(score) from studentcourse)) as b on student.id=b.student_id ; +-- 2.查询编号是2的课程比编号是1的课程最高成绩高的学生信息。 + -- 2.1在中间表查询课程编号是1的最高分数 -- 80 + SELECT max(score) from studentcourse WHERE course_id=1; + -- 2.2在中间表中查询课程编号是2并且分数大于上述结果的学生编号 -- 多行单列 + SELECT * from studentcourse WHERE course_id=2 and score>(SELECT max(score) from studentcourse WHERE course_id=1); + -- 2.3 在学生表中根据上述查询的学生编号查询学生信息 + SELECT * from student JOIN (SELECT * from studentcourse WHERE course_id=2 and score>(SELECT max(score) from studentcourse WHERE course_id=1)) as b on student.id=b.student_id; + -- 3.查询编号是2的课程比编号是1的课程最高成绩高的学生姓名和成绩。 + SELECT max(score) from studentcourse WHERE course_id=1; + SELECT score from studentcourse WHERE course_id=2 and score>(SELECT max(score) from studentcourse WHERE course_id=1); + -- 3.1在中间表查询课程编号是1的最高分数 -- 80 + SELECT max(score) from studentcourse WHERE course_id=1; + -- 3.2在中间表中查询课程编号是2并且分数大于上述结果的学生编号和分数 -- 多行多列作为临时表 + SELECT score from studentcourse WHERE course_id=2 and score>(SELECT max(score) from studentcourse WHERE course_id=1); + -- 3.3 将上述查询的结果作为临时表 + SELECT score from studentcourse WHERE course_id=2 and score>(SELECT max(score) from studentcourse WHERE course_id=1); +-- 4.查询每个同学的学号、姓名、选课数、总成绩 + -- 4.1在中间表中根据学号分组查询学号 选课数 总成绩 -- 多行多列,作为临时表 + SELECT * from + -- 4.2将上述查询的结果作为临时表和学生表进行关联查询 + + +``` + +```sql +-- 1.查询所有员工信息。显示员工编号,员工姓名,工资,职务名称,职务描述。 + -- 1.1 确定几张表关联查询:2张表 emp job + SELECT * from emp JOIN job on emp.job_id=job.id; + -- 1.2 确定连接查询的条件即避免笛卡尔积的条件 emp.job_id = job.id + -- 1.3 确定要查询的结果字段:员工编号,员工姓名,工资,职务名称,职务描述 + SELECT emp.id,ename,salary,jname,description from emp JOIN job on emp.job_id=job.id; + +-- 2.查询所有员工信息。显示员工编号,员工姓名,工资,职务名称,职务描述,部门名称,部门位置 + -- 2.1 确定几张表关联查询:3张表 emp job dept + SELECT * from emp JOIN job on job.id=emp.job_id JOIN dept on emp.dept_id=dept.id; + -- 2.2 确定连接查询的条件即避免笛卡尔积的条件 emp.job_id = job.id and emp.dept_id=dept.id + -- 2.3 确定要查询的结果字段:员工编号,员工姓名,工资,职务名称,职务描述,部门名称,部门位置 + SELECT emp.id,ename,salary,jname,description,dname,loc from emp JOIN job on job.id=emp.job_id JOIN dept on emp.dept_id=dept.id; + +/* + 连接查询规律: + 1.确定几张表关联 + 2.确定连接查询的条件 + 3.确定要查询的结果字段 + 4.如果是n张表关联,那么避免笛卡尔积的条件的个数是:n-1 + */ + + +-- 3.查询所有员工信息。显示员工姓名,工资,职务名称,职务描述,部门名称,部门位置,工资等级。 + -- 1.确定几张表关联 4张表 emp job dept salarygrade + SELECT * from emp JOIN job on job.id=emp.job_id JOIN dept on emp.dept_id=dept.id JOIN salarygrade on + -- 2.确定连接查询的条件 :emp.job_id = job.id and emp.dept_id=dept.id + -- 其他条件:e.salary between salarygrade.losalary and salarygrade.hisalary + -- 3.确定要查询的结果字段 + + +``` + -- Gitee