From c28d0b6617ad45e96493453721e131a39fbdb57f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E4=BD=B3=E6=80=A1?= <3197656261@qq.com> Date: Wed, 22 Mar 2023 22:27:16 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=8D=E4=B9=A0=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2023.03.22 \344\275\234\344\270\232.md" | 455 ++++++++++++++++++ 1 file changed, 455 insertions(+) create mode 100644 "49.\346\235\216\344\275\263\346\200\241/2023.03.22 \344\275\234\344\270\232.md" diff --git "a/49.\346\235\216\344\275\263\346\200\241/2023.03.22 \344\275\234\344\270\232.md" "b/49.\346\235\216\344\275\263\346\200\241/2023.03.22 \344\275\234\344\270\232.md" new file mode 100644 index 0000000..12e5e6e --- /dev/null +++ "b/49.\346\235\216\344\275\263\346\200\241/2023.03.22 \344\275\234\344\270\232.md" @@ -0,0 +1,455 @@ +drop DATABASE if exists fruits; + +create database fruits charset utf8; +use fruits; + +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); + +# 查询水果和价格信息 + +SELECT b.name,a.price from price a INNER JOIN fruit b on a.id=b.id; + + + +SELECT * from price a INNER JOIN price b on a.id=b.id; + +# 查询苹果信息,显示苹果id和价格 + +SELECT * from price where id=(SELECT id FROM fruit where name='苹果'); + +-- 查询所有水果信息和对应价格信息 + +SELECT b.name,a.price from price a INNER JOIN fruit b on a.id=b.id; + +-- fruit f 位于left左边,称为左表,左外连接以左表为主 + +-- f.* :获取fruit水果表中的所有数据 + +SELECT f.* from fruit f left join price p on f.id=p.id; + +-- 获取price价格表的字段price的值 +SELECT price from price; + +-- 查询没有写价格的水果及价格(先查询所有水果的价格信息,再筛选。) + +SELECT * from fruit f right join price p on f.id=p.id where price_id is null or price is null; + +-- 使用右外连接查询所有价格对应的水果名称和价格信息, + +SELECT * from fruit f right join price p on f.id=p.id ; + +-- 查询出没有对应水果的价格编号和价格,水果名 + +SELECT * from fruit f right join price p on f.id=p.id where price_id is null or price is null; + + +-- 练习:使用左外连接查询价格对应的水果,显示所有价格 + +SELECT f.name,p.price from fruit f left join price p on f.id=p.id; + +-- 创建部门表 1 +CREATE TABLE dept ( + id INT PRIMARY KEY AUTO_INCREMENT, -- 部门编号 + NAME VARCHAR(20) -- 部门名称 +); + +INSERT INTO dept (NAME) VALUES ('开发部'),('市场部'),('财务部'); + +-- 创建员工表 n +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 * from emp where salary 5000); + + + -- 3.2在dept部门表中根据上述查询的部门编号查询部门名字 +SELECT * from dept where id in +(SELECT dept_id from emp where salary >5000); + + +-- 4. 查询开发部与财务部所有的员工信息 + +SELECT * from emp where dept_id in +(SELECT id from dept where name='开发部' or name='财务部'); + + -- 4.1 在dept表查询开发部和财务部的部门编号 -- 1 3 多行单列 作为子查询的条件使用in + SELECT * from emp where dept_id in(1,3); + + +​ +​ + -- 4.2在emp员工表中根据上述查询的部门编号查询员工信息 + + SELECT * from emp where dept_id in +(SELECT id from dept where name='开发部' or name='财务部'); + + +-- 5.查询出2011年以后入职的员工信息,包括部门名称 + -- 5.1在emp表中查询2011年以后入职的员工信息 -- 多行多列,作为子查询一般使用as起别名作为临时表和其他表查询. + -- 5.2 将上述查询的结果作为临时表和dept表关联查询最后查询员工信息和部门名称 + + + SELECT e.*,d.`NAME` from emp e INNER JOIN dept d on e.dept_id =d.id where e.join_date>='2011-1-1'; + + +-- 5.多表练习 子查询 +-- 教师表 + create table teacher ( + id int(11) primary key auto_increment, -- 教师编号 + name varchar(20) not null unique -- 教师姓名 + ); + +-- 学生表 + create table student ( + id int(11) primary key auto_increment, -- 学生编号 + name varchar(20) NOT NULL unique, -- 学生姓名 + city varchar(40) NOT NULL, -- 学生城市 + age int -- 学生年龄 + ) ; + +-- 课程信息表 + 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) + ); + +-- 学生选课表 + 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 teacher values(null,'关羽'); +insert into teacher values(null,'张飞'); +insert into teacher values(null,'赵云'); + +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); + +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); + +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查询学生信息 + SELECT * from student + where id=(SELECT student_id from studentcourse where score = + (SELECT max(score) from studentcourse)); + -- 一个班级最高分95,可以有多名学员,结果有可能是多行多列 + + + + +-- 2.查询编号是2的课程比编号是1的课程最高成绩高的学生信息。 + -- 2.1在中间表查询课程编号是1的最高分数 -- 80 + SELECT max(score) from studentcourse where course_id=1; + + -- 2.2在中间表中查询课程编号是2并且分数大于上述结果的学生编号 -- 多行单列 + SELECT student_id from studentcourse where course_id=2 and score> + (SELECT max(score) from studentcourse where course_id=1); + -- 2.3 在学生表中根据上述查询的学生编号查询学生信息 +SELECT * from student where id in +( SELECT + student_id + FROM + studentcourse + WHERE + course_id = 2 + AND score > ( SELECT max( score ) FROM studentcourse WHERE course_id = 1 )); + +-- 3.查询编号是2的课程比编号是1的课程最高成绩高的学生姓名和成绩。 +SELECT + stu.NAME, + score +FROM + student stu + INNER JOIN studentcourse rse ON stu.id = rse.student_id +WHERE +( + SELECT + max( score ) + FROM + studentcourse + WHERE + course_id = 1 + ) +