diff --git "a/49 \346\235\216\350\210\222\346\261\266/20230321\345\222\21420230323\344\275\234\344\270\232.md" "b/49 \346\235\216\350\210\222\346\261\266/20230321\345\222\21420230323\344\275\234\344\270\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..de32a2236488573d5201ed020519de05b6d7802a --- /dev/null +++ "b/49 \346\235\216\350\210\222\346\261\266/20230321\345\222\21420230323\344\275\234\344\270\232.md" @@ -0,0 +1,685 @@ +```mysql + +``` +-- 0.创建数据库练习 +-- 1.直接创建数据库db3 +create database db3 charset utf8; +-- 2.判断是否存在并创建数据库db4 +create database if not exists db4 charset utf8; +-- 3.查看所有的数据库 +show databases; +-- 4.删除db3数据库 +drop database db3; +-- 5.查看正在使用的数据库 +select database(); +-- 6.使用db4数据库 +use db4; +-- +-- +-- 1.创建表练习 +-- - 需求:设计一张学生表,请注重数据类型、长度的合理性 +create table stu( + id int, -- 编号 + name varchar(10), -- 姓名 + sex char, -- 性别 + age date, -- 生日 + score double(5,2), -- 入学成绩 + email varchar(64), -- 邮件地址 + tel varchar(20), -- 家庭联系电话 + state int -- 学生状态 +); +-- +-- 1.编号 +-- 2.姓名,姓名最长不超过10个汉字 +-- 3.性别,因为取值只有两种可能,因此最多一个汉字 +-- 4.生日,取值为年月日 +-- 5.入学成绩,小数点后保留两位 +-- 6.邮件地址,最大长度不超过 64 +-- 7.家庭联系电话,不一定是手机号码,可能会出现 - 等字符 +-- 8.学生状态(用数字表示,正常、休学、毕业...) +-- 2.更改表练习 +-- -- 1.修改stu表名为student +alter table stu rename to student; +-- -- 2.给学生表添加一列remark,类型为varchar(20) +alter table student add remark varchar(20); +-- -- 3.修改remark列的类型是varchar(100) +alter table student modify remark varchar(100); +-- -- 4.修改remark列的名变为intro,类型varchar(30) +alter table student change remark intro varchar(30); +-- -- 5.删除intro列 +alter table student drop column intro; +-- 3.添加数据 +-- -- 1.创建表 +-- +-- -- 2.插入部分数据,给id name age sex赋值 +insert into student (id,name,age,sex) values + (1,'张三','2001-06-06','男'), + (2,'李四','2004-08-08','女'), + (3,'张午','2005-07-04','男'), + (4,'陈留','2002-11-11','男'), + (5,'齐朱','2003-01-16','女'); +-- /* +-- 1.在mysql中插入字符数据,数据使用单引号或者双引号都可以,建议使用单引号 +-- 2.在mysql中插入的数据是date类型,那么将数据书写在单引号或者双引号都可以 +-- */ +-- +-- -- 3.插入所有的字段,不写字段名 +insert into student values (1,'张琪','女','2023-03-11',100.00,'集美大学','666-666-666',0); +-- +-- -- 4.插入所有的字段,写出字段名 +insert into student + (id,name,sex,age,score,email,tel,state) + values + (1,'张琪','女','2023-03-11',100.00,'集美大学','666-666-666',0); +-- -- 5.批量插入数据 +insert into student values + (1,'张琪','女','2023-03-11',100.00,'集美大学','666-666-666',0), + +(2,'张琪','女','2023-03-11',100.00,'集美大学','666-666-666',0); +-- -- 插入数据注意 +-- -- 6.插入数据时 省略列名,那么如果不需要给列赋值那么使用null +insert into student values +(1,'张琪','女','2023-03-11',100.00,'集美大学',null,0), + +(2,'张琪','女','2023-03-11',100.00,'集美大学','666-666-666',0); +-- 4.修改数据 +-- +-- -- 1.修改数据:修改性别都变为女 +update student set sex = '女'; +-- -- 2.修改数据:带条件修改id是2的学生性别变为男 +update student set sex = '男' where id = 2; +-- -- 3.一次修改多个列,把id为3的学生,年龄改成26岁,address改成北京 +update student set age = 26 , address = '北京' where id = 3; +-- +-- 5.删除数据 +-- -- 1.带条件删除数据,删除id是3的记录 +delete from student where id = 3; +-- +-- -- 2.带条件删除数据,删除id是1和2的记录 id in(1,2) : 表示id的值是1或者2 +delete from student where id in (1,2); +-- +-- +-- -- 3.不带条件删除 +delete from student; +-- -- 还可以使用:DDL +-- -- 这里的table可以省略 +-- +-- 6.查询数据 +-- +-- +-- -- 1.查询多个字段 +select * from student; +-- -- * 表示所有字段 +-- +-- -- 2.写出查询每列的名称 +select id,name,sex,age,score,email,tel,state from student; +-- +-- -- 3.查询表中name和age列,name列的别名为姓名,age列的别名为年龄 +select name 姓名,age 年龄 from student +-- -- 使用as起给字段和表起别名 +-- -- as可以省略 +-- +-- +-- -- 4.去重 +select distinct age from student; +-- -- 对age去重 +-- +-- +-- -- 对age和address去重 +select distinct age,address from student; +-- -- 如果多个字段一起去重,如果所有的字段值全部一样才会去重 +-- +-- +-- +-- 7.带条件的查询 + +-- -- 1.查询数学成绩大于80学生 +select * from student where score> 80; +-- +-- -- 2.查询英语成绩小于等于80学生 +select * from student where score < 80; +-- +-- -- 3.查询年龄等于20的学生 +select * from student where age = 20; +-- +-- -- 4.查询年龄不等于20的学生 +select * from student where age != 20; +-- +-- +-- -- 5.查询年龄大于35并且性别是男的学生 +select * from student where age > 35 and sex = '男'; +-- +-- -- 6.查询年龄大于35或者性别是男的学生 +select * from student where age > 35 or sex = '男'; +-- +-- -- 7.查询id是1 3 5的学生 +select * from student where id in (1,3,5); +-- +-- +-- -- 8.查询id不是1 3 5的学生 +select * from student where id not in (1,3,5); +-- -- not in 是in的取反,表示不在什么其中 +-- +-- -- 范围查询 +-- -- 9.英语成绩在75和90之间 +select * from student where score between 75 and 90; +-- +-- +-- -- 模糊查询 使用like +-- -- 10.查询姓赵的学生 +select * from student where name like '赵%'; +-- -- name like '赵%' 表示name的值以赵开始,后面是什么都可以 +-- -- %在模糊查询中表示任意个数的字符 +-- +-- +-- -- 11.查询包含岩 +select * from student where name like '%岩%'; +-- -- name like '%岩%' 表示name的值只要含有岩即可,前后%表示匹配任意多个字符 +-- +-- +-- -- 12.查询姓赵并且是三个字的学生 +select * from student where name like '赵__'; +-- -- _下划线在模糊查询中表示单个字符 +-- +-- +-- -- 排序 +-- -- desc表示降序 +-- -- 13.按照年龄降序排序 +select * from student order by age desc; +-- +-- -- 14.先按照年龄降序排序,如果年龄相同在按照数学降序排序 +select * from student order by age,score desc; +-- +-- -- 15.先按照年龄降序排序,如果年龄相同在按照数学升序排序 +select * from student order by age desc,score asc; +-- +-- +-- -- 聚合函数 +-- -- 16.查询学生总数即有多少名学生 +select count(id) from student; +-- +-- -- 17.查询数学成绩总分数 +select sum(score) from student; +-- +-- -- 18.查询数学成绩最高分数 +select max(score) from student; +-- +-- -- 19.统计数学和英语总和值 +-- -- 实现一:分别统计数学和英语的每个总成绩,然后进行相加 +select sum(maths) + sum(english) from student; +-- +-- +-- -- 实现二:分别统计每个人的数学和英语成绩,然后将每个人的数学和英语成绩相加 +select sum(maths+english) from student; +-- /* +-- 结果是:380 少了柳岩的90 +-- sum(math+english): +-- 问题原因: +-- +-- +-- +-- 解决上述问题:使用mysql自带函数:ifnull(字段值,默认值) +-- 说明: +-- ifnull(字段值,默认值) +-- 如果字段值是null,那么使用默认值作为ifnull函数的整体结果 +-- 如果字段值不是null,那么使用字段值作为ifnull函数的整体结果 +-- 举例: +-- ifnull(english,0): +-- 1)假设english的值是null===>ifnull(null,0)===>整体结果是0 +-- 2)假设english的值是80===>ifnull(80,0)===>整体结果是80 +-- */ +-- +-- +-- +-- -- 分组 +-- -- 准备数据 + create table car( + id int, + color char(2), + price float + ); + insert into car(id,color,price) values(1,'黄色',16); + insert into car(id,color,price) values(2,'黄色',16); + insert into car(id,color,price) values(3,'蓝色',5); + insert into car(id,color,price) values(4,'红色',60); + insert into car(id,color,price) values(5,'白色',8); + insert into car(id,color,price) values(6,'红色',60); + +-- +-- -- 1.查询每种颜色车辆的总价 +select color,sum(price) from car group by color; +-- +-- -- 2.查询每种颜色车辆总价大于30车辆的颜色和总价 +select color,sum(price) from car group by color having sum(price) > 30; +-- +-- -- 3.注意:分组查询的结果最好是分组字段和聚合函数,不要是其他字段 +-- +-- -- 4.where后面不能使用聚合函数 报错的 +-- +-- -- 小结:where是在分组前筛选,having是在分组后筛选 +-- -- 5.查询车的单价大于15的每种颜色车辆总价大于30车辆的颜色和总价 +select color,sum(price) from car where price >15 group by color having sum(price) > 30; +-- +-- +-- -- 分页 +-- -- 1.分页查询数据,每页显示2条数据 +select * from car limit 2; +-- -- 第一页 +-- -- 0 表示起始索引,对应第一行数据 +-- -- 2表示每页显示2条数据 +-- +-- +-- -- 当起始索引是0的情况下可以不写索引0,写法如下, +-- -- 下面的2表示每页显示的数据行数 +-- +-- -- 第二页 +select * from car limit 2,2; +-- -- 第一个2表示起始索引,对应三行数据 +-- -- 第二个2表示每页显示两条数据,对于第二个参数每个公司基本是固定的,最后一页剩下几行数据 +-- -- 就会显示几行数据 +-- +-- -- 第三页 +select * from car limit 4,2; +-- /* +-- 公式: 起始索引 = (当前页码-1)*每页显示的条数 +-- 0 1 2 +-- 2 2 2 +-- 4 3 2 +-- */ + + + +``` + +```mysql +# 1 创建数据库,并设置字符集 + +create database hw charset utf8; +# 2 使用数据库 +use hw; + +# 3 创建表结构 创建一个就插入一个表的数据 + +-- 创建价格表结构 +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 * from price; +select * from fruit; +-- 3.多表查询 + -- 3.1.笛卡尔积 + -- 需求:查询水果和价格信息 + select * from fruit a left join price b on a.price_id = b.id; + +-- 3.2.内连接 +-- 避免笛卡尔积问题怎么处理? + + +-- 练习:查询苹果信息,显示苹果id和价格 +select a.id ,b.price from fruit a left join price b on a.price_id = b.id +where +a.name = '苹果'; +-- 分析:在水果表中可以查看苹果id 名字 ,在价格表中可以查看价格和价格id + + +-- 3.3左外连接 +-- 练习:查询所有水果信息和对应价格信息 +select * from fruit a left join price b on a.price_id = b.id; +-- fruit f 位于left左边,称为左表,左外连接以左表为主 +-- price p 位于left右边,称为右表 +-- f.* :获取fruit水果表中的所有数据 +-- p.price : 获取price价格表的字段price的值 + + +-- 练习:查询没有写价格的水果及价格(先查询所有水果的价格信息,再筛选。) +select * from fruit a left join price b on a.price_id = b.id where a.price_id is null; + + +-- 3.4右外连接 +-- 练习:使用右外连接查询所有价格对应的水果名称和价格信息, +select * from fruit a right join price b on a.price_id = b.id; +-- right join 表示右连接 +-- price p 称为右表 右外连接会查询右表即price表的全部数据以及和左表fruit f的交集 + + +-- 练习:查询出没有对应水果的价格编号和价格,水果名 +select * from fruit a right join price b on a.price_id = b.id where price_id is null; + +-- 练习:使用左外连接查询价格对应的水果,显示所有价格 +select * from fruit a left join price b on a.price_id = b.id; + + +-- 4.子查询 + + -- 创建部门表 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); + +select * from dept; +select * from emp; + +-- 1.查询工资最高的员工是谁 + -- 1.1 在emp员工表中查询最高工资 -- 9000 单行单列 + select max(salary) from emp; + -- 1.2在emp员工表中根据上述查询的最高工资查询员工信息 +select * from emp where salary = (select max(salary) from emp); + +-- 2.查询工资小于平均工资的员工有哪些? + -- 2.1在emp员工表中查询平均工资 -- 单行单列 5860 + select avg(salary) from emp; + -- 2.2在emp员工表中查询小于上述的平均工资查询员工信息 +select * from emp where salary < (select avg(salary) from emp); + +-- 3.查询工资大于5000的员工,来自于哪些部门,显示部门的名字 + -- 3.1 在员工表emp中查询工资大于5000的员工部门编号 -- 1 2 单列多值 作为子查询的条件使用in + select dept_id from emp where salary > 5000; + -- 3.2在dept部门表中根据上述查询的部门编号查询部门名字 +select * from dept where id in (select dept_id from emp where salary > 5000); + +-- 4. 查询开发部与财务部所有的员工信息 + -- 4.1 在dept表查询开发部和财务部的部门编号 -- 1 3 多行单列 作为子查询的条件使用in + select id from dept where name = '开发部' or name = '财务部'; + -- 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起别名作为临时表和其他表查询. + select * from emp where join_date > '2011-12-31'; + -- 5.2 将上述查询的结果作为临时表和dept表关联查询最后查询员工信息和部门名称 +select * from (select * from emp where join_date > '2011-12-31') a inner join dept b on a.dept_id = b.id; + +-- 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); + + +select * from teacher; +select * from student; +select * from course; +select * from studentcourse; +-- 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 where id = (select student_id from studentcourse where score = (select max(score) from studentcourse)); + +-- 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的课程最高成绩高的学生姓名和成绩。 + -- 3.1在中间表查询课程编号是1的最高分数 -- 80 + select max(score) from studentcourse where course_id = 1; + -- 3.2在中间表中查询课程编号是2并且分数大于上述结果的学生编号和分数 -- 多行多列作为临时表 + select student_id,score from studentcourse where course_id = 2 and score > (select max(score) from studentcourse where course_id = 1); + -- 3.3 将上述查询的结果作为临时表 +select b.name,a.score from (select student_id,score from studentcourse where course_id = 2 and score > (select max(score) from studentcourse where course_id = 1)) a inner join student b on a.student_id = b.id; + +-- 4.查询每个同学的学号、姓名、选课数、总成绩 + -- 4.1在中间表中根据学号分组查询学号 选课数 总成绩 -- 多行多列,作为临时表 + select student_id 学号,count(student_id) 选课数,sum(score) 总成绩 from studentcourse group by student_id having sum(score); + -- 4.2将上述查询的结果作为临时表和学生表进行关联查询 +select a.`学号`,b.name,a.`选课数`,a.`总成绩` from (select student_id 学号,count(student_id) 选课数,sum(score) 总成绩 from studentcourse group by student_id having sum(score)) a inner join student b on a.学号 = b.id; + +/* + 注意:如果子查询中查询的结果字段是聚合函数,并且最后结果需要使用聚合函数,那么必须使用as给聚合函数的字段起别名 + */ + + + + +-- 6.多表练习 连接查询 + -- 部门表 + CREATE TABLE dept ( + id INT PRIMARY KEY PRIMARY KEY, -- 部门id + dname VARCHAR(50), -- 部门名称 + loc VARCHAR(50) -- 部门位置 + ); + + -- 添加4个部门 + INSERT INTO dept(id,dname,loc) VALUES + (10,'教研部','北京'), + (20,'学工部','上海'), + (30,'销售部','广州'), + (40,'财务部','深圳'); + + -- 职务表,职务名称,职务描述 + CREATE TABLE job ( + id INT PRIMARY KEY, -- 职务编号 + jname VARCHAR(20), -- 职务名称 + description VARCHAR(50) -- 职务简介 + ); + + -- 添加4个职务 + INSERT INTO job (id, jname, description) VALUES + (1, '董事长', '管理整个公司,接单'), + (2, '经理', '管理部门员工'), + (3, '销售员', '向客人推销产品'), + (4, '文员', '使用办公软件'); + + -- 员工表 + CREATE TABLE emp ( + id INT PRIMARY KEY, -- 员工id + ename VARCHAR(50), -- 员工姓名 + job_id INT, -- 职务id + mgr INT , -- 上级领导 + joindate DATE, -- 入职日期 + salary DECIMAL(7,2), -- 工资 + bonus DECIMAL(7,2), -- 奖金 + dept_id INT, -- 所在部门编号 + CONSTRAINT emp_jobid_ref_job_id_fk FOREIGN KEY (job_id) REFERENCES job (id), + CONSTRAINT emp_deptid_ref_dept_id_fk FOREIGN KEY (dept_id) REFERENCES dept (id) + ); + + -- 添加员工 + INSERT INTO emp(id,ename,job_id,mgr,joindate,salary,bonus,dept_id) VALUES + (1001,'孙悟空',4,1004,'2000-12-17','8000.00',NULL,20), + (1002,'卢俊义',3,1006,'2001-02-20','16000.00','3000.00',30), + (1003,'林冲',3,1006,'2001-02-22','12500.00','5000.00',30), + (1004,'唐僧',2,1009,'2001-04-02','29750.00',NULL,20), + (1005,'李逵',4,1006,'2001-09-28','12500.00','14000.00',30), + (1006,'宋江',2,1009,'2001-05-01','28500.00',NULL,30), + (1007,'刘备',2,1009,'2001-09-01','24500.00',NULL,10), + (1008,'猪八戒',4,1004,'2007-04-19','30000.00',NULL,20), + (1009,'罗贯中',1,NULL,'2001-11-17','50000.00',NULL,10), + (1010,'吴用',3,1006,'2001-09-08','15000.00','0.00',30), + (1011,'沙僧',4,1004,'2007-05-23','11000.00',NULL,20), + (1012,'李逵',4,1006,'2001-12-03','9500.00',NULL,30), + (1013,'小白龙',4,1004,'2001-12-03','30000.00',NULL,20), + (1014,'关羽',4,1007,'2002-01-23','13000.00',NULL,10); + + -- 工资等级表 + CREATE TABLE salarygrade ( + grade INT PRIMARY KEY, + losalary INT, -- 最低薪资 + hisalary INT -- 最高薪资 + ); + + -- 添加5个工资等级 + INSERT INTO salarygrade(grade,losalary,hisalary) VALUES + (1,7000,12000), + (2,12010,14000), + (3,14010,20000), + (4,20010,30000), + (5,30010,99990); + + select * from dept; + select * from job; + select * from emp; + select * from salarygrade; + +-- 1.查询所有员工信息。显示员工编号,员工姓名,工资,职务名称,职务描述。 + -- 1.1 确定几张表关联查询:2张表 emp job + -- 1.2 确定连接查询的条件即避免笛卡尔积的条件 emp.job_id = job.id + -- 1.3 确定要查询的结果字段:员工编号,员工姓名,工资,职务名称,职务描述 + select a.id,a.ename,a.salary,b.jname,b.description from emp a left join job b on a.job_id = b.id; + + +-- 2.查询所有员工信息。显示员工编号,员工姓名,工资,职务名称,职务描述,部门名称,部门位置 + -- 2.1 确定几张表关联查询:3张表 emp job dept + -- 2.2 确定连接查询的条件即避免笛卡尔积的条件 emp.job_id = job.id and emp.dept_id=dept.id + -- 2.3 确定要查询的结果字段:员工编号,员工姓名,工资,职务名称,职务描述,部门名称,部门位置 +select a.id,a.ename,a.salary,b.jname,b.description,c.dname,c.loc from emp a left join job b on a.job_id = b.id left join dept c on a.dept_id = c.id; + +/* + 连接查询规律: + 1.确定几张表关联 + 2.确定连接查询的条件 + 3.确定要查询的结果字段 + 4.如果是n张表关联,那么避免笛卡尔积的条件的个数是:n-1 + */ + + +-- 3.查询所有员工信息。显示员工姓名,工资,职务名称,职务描述,部门名称,部门位置,工资等级。 + -- 1.确定几张表关联 4张表 emp job dept salarygrade + -- 2.确定连接查询的条件 :emp.job_id = job.id and emp.dept_id=dept.id + -- 其他条件:e.salary between salarygrade.losalary and salarygrade.hisalary + -- 3.确定要查询的结果字段 +select a.id,a.ename,a.salary,b.jname,b.description,c.dname,c.loc from emp a left join job b on a.job_id = b.id left join dept c on a.dept_id = c.id; + + + +-- 4.查询出每个部门的部门编号、部门名称、部门位置、部门人数 + -- 4.1 在emp表中按照部门编号分组查询部门编号和每个部门人数 + select dept_id 部门编号,count(dept_id) 部门人数 from emp group by dept_id; + -- 4.2 几张表关联:dept 上述临时表 + -- 4.3 条件 dept.id和临时表的dept_id相等 + -- 4.4 分析查询的结果部门编号、部门名称、部门位置、部门人数 +select a.`部门编号`,b.dname 部门名称,b.loc 部门位置,a.`部门人数` from (select dept_id 部门编号,count(dept_id) 部门人数 from emp group by dept_id) a left join dept b on a.`部门编号` = b.id; +``` + +