#eg: 修改id为1的数据,将name修改为9tse update emp set name ='9tse'where id =1; # 修改id为1的数据,将name修改为 sewer,gender修改为 女 update emp set name ='sewer',gender ='女'where id =1; # 将所有员工的入职日期修改为2008-01-01 update emp set entrydate ='2008-01-01';
-- 需求:查询emp表的员工姓名和工作地址(北京/上海————>一线城市,其他————>二线城市) select name, (case workaddress when '北京' then '一线城市' when'上海' then '一线城市' else '二线城市'end)as'工作地址' from emp;
select id, name, (casewhen math >=85then'优秀'when math >=60then'及格'else'不及格'end)'数学', (casewhen english >=85then'优秀'when english >=60then'及格'else'不及格'end)'英语', (casewhen chinese >=85then'优秀'when chinese >=60then'及格'else'不及格'end)'语文', from score;
#内连接查询语法 -- 隐式内连接 select 字段列表 from 表1,表2where 条件...; -- 显式内连接 select 字段列表 from 表1 [inner] join 表2on 连接条件...;
eg:
1 2 3 4 5 6 7 8 9 10 11 12
-- 1.查询每一个员工的姓名,及关联的部门的名称(隐式内连接实现) -- 表结构:emp,dept -- 连接条件:emp.dept_id=dept.id select emp.name,dept.name from emp,dept where emp.dept_id = dept.id; select e.name,d.name from emp e , dept d where e.dept_id = d.id;
-- 2.查询每一个员工的姓名,及关联的部门的名称(显式内连接实现)---- INNER JOIN..ON// -- 表结构:emp,dept -- 连接条件:emp.dept_id=dept.id select e.name,d.name from emp e innerjoin dept d on e.dept_id = d.id; select e.name,d.name from emp e join dept d on e.dept_id = d.id;
-- 1 查询emp表的所有数据,和对应的部门信息(左外连接 -- 表结构:emp,dept -- 连接条件:emp.dept_id=dept.id select e.*,d.name from emp e leftouterjoin dept d on e.dept_id = d.id; select e.*,d.name from emp e leftjoin dept d on e.dept_id =d.id;
-- 2 查询dept表的所有数据,和对应的员工信息(右外连接) select d.* , e. from emp e rightouterjoin dept d on e.dept_id = d.id; select d.* , e. from dept d leftouterjoin emp e on e.dept_id = d.id;
5.1.3 自连接
1 2 3
#自连接查询语法: select 字段列表 from 表A 别名A join 表A 别名B on 条件...; #自连接查询,可以是内连接查询,也可以是外连接查询
eg:
1 2 3 4 5 6 7 8
-- 1.查询员工及其所属领导的名字 -- 表结构:emp select a.name , b.name from emp a , emp b where a.managerid = b.id;
-- 2,查询所有员工emp及其领导的名字emp,如果员工没有领导,也需要查询出来 -- 表结构:emp a,emp b select a.name '员工' , b.name '领导'from emp a leftjoin emp b on a.managerid = b.id;
5.2 联合查询
对于union查询,就是把多次查询的结果合并起来,形成一个新的查询结果集
1 2 3
select 字段列表 from 表a ... union [all] select 字段列表 from 表b ...;
ps :
对于联合查询的多张表的列数必须保持一致,字段类型也需要保持一致。
unionall会将全部的数据直接合并在一起, union会对合并之后的数据去重。
eg :
1 2 3 4 5 6 7 8 9 10 11
-- union all , union -- 1.将薪资低于50的员工,和年龄大于50岁的员工全部查询出来。 select*from emp Where salary<500 unionall select*from emp where age>50; # 查询出的结果有可能出现重复的行
select*from emp where salary<5000 union select*from emp where age >50; #去掉 all 就可以去重
-- 列子查询 -- 1. 查询“销售部”和“市场部”的所有员工信息 -- a. 查询“销售部”和“市场部”的部门ID select id from dept where name ='销售部'or name ='市场部'; -- b. 根据部门ID,查询员工信息 select*from emp where dept_id in (select id from dept where name ='销售部'or name ='市场部');
-- 2. 查询比财务部所有工资都高的员工信息 -- a. 查询所有财务部人员工资 select id from dept where name ='财务部'; select salary from emp where dept_id =(select id from dept where name ='财务部'); -- b.比财务部所有人工资都高的员工信息 select*from emp where salary >all (select salary from emp where dept_id = (select id from dept where name ='财务部'));
-- 3. 查询比研发部其中任意一人工资高的员工信息 -- a. 查询研发部所有人工资 select salary from emp where dept_id = (select id from dept where name ='研发部'); -- b. 比研发部其中任意一人工资高的员工信息 select*from emp Where salary >some (select salary from emp where dept_id = (select id from dept where name '研发部'));
5.3.3 行子查询
子查询返回的结果是一行(可以是多列),这种子查询称为行子查询 常用的操作符:= , <> , in , not in
eg :
1 2 3 4 5
-- 1. 查询与“张无”的薪资及直属领导相同的员工信息 -- a. 查询“张无”的薪资及直属领导 select salary,managerid from emp where name ='张无' -- b. 查询与“张无”的薪资及直属领导相同的员工信息 select*from emp where (salary,managerid)=( select salary, managerid from emp where name ='张无');
5.3.4 表子查询
子查询返回的结果是多行多列,这种子查询称为表子查询 常用的操作符:IN
eg :
1 2 3 4 5 6 7 8 9 10 11 12 13
-- 表子查询 -- 1. 查询与“鹿客”“宋远桥”的职位和薪资相同的员工信息 -- a. 查询“鹿枝客”,“宋远桥”的职位和薪资 select job,salary from emp where name ='鹿客'or name ='宋远桥'; -- b. 查询与“鹿客”,“宋远桥”的职位和薪资相同的员工信息 select*from emp where (job,salary) in (select job,salary from emp where name ='鹿杖客'or name ='宋远桥');
-- 2. 查询入职日期是“2086-01-01之后的员工信息,及其部门信息 -- a. 入职日期是“206-1-01”之后的员工信息 select*from emp where entrydate >'2006-01-01'; -- b. 查询这部分员工,对应的部门信息 select e.*,d.*from (selectfrom emp where entrydate >'2006-01-01')e leftjoin dept d on e.dept_id = d.id;
-- 1. 查询员工的姓名、年龄、职位、部门信息 (隐式内连接) -- 表:emp,dept -- 连接条件:emp.dept_id = dept.id select e.name , e.age , e.job , d.name from emp e , dept d where e.dept_id=d.id;
-- 2. 查询年龄小于3岁的员工的姓名、年龄、职位、部门信息(显式内连接) -- 表:emp,dept -- 连接条件:emp.dept_id=dept.id select e.name , e.age , e.job , d.name from emp e innerjoin dept d on e.dept_id = d.id where e.age <30;
-- 3.查询拥有员工的部门ID、部门名称 -- 表:emp,dept -- 连接条件:emp.dept_id=dept.i selectdistinct d.id , d.name from emp e , dept d where e.dept_id = d.id;
-- 4. 查询所有年龄大于4岁的员工,及其归属的部门名称;如果员工没有分配部门,也需要展示出来 -- 表:emp,dept -- 连接条件:emp.dept_id=dept.id -- 外连接 select e.* , d.name from emp e Leftjoin dept d on e.dept_id = d.id where e.age >40;
-- 5. 查询所有员工的工资等级 -- 表:emp,sagrade -- 连接条件:emp.salary>= salgrade.losal and demp.salary <= sagrade.hisal select e.* , s.grade , s.losal , s.hisal from emp e , salgrade s where e.salary >= s.losa and e.salary <= s.hisal; select e.* , s.grade , s.losal , s.hisal from emp e , salgrade s where e.salary between s.losal and s.hisal;
-- 6. 查询“研发部”所有员工的信息及工资等级 -- 表:emp,salgrade,dept -- 连接条件:emp.salary between salgrade.losal and salgrade.hisal , emp.dept_id = dept.id -- 查询条件:dept.name= '研发部' select e.* , s.grade from emp e , dept d , salgrade s where e.dept_id = d.id and (e.salary between s.losal and s.hisal) and d.name ='研发部';
-- 7. 查询“研发部”员工的平均工资 -- 表:emp,dept -- 连接条件: emp.dept_id = dept.id selectavg(e.salary) from emp e , dept d where e.dept_id = d.id and d.name ='研发部';
-- 8. 查询工资比“灭绝”高的员工信息 -- a. 查询“灭绝”的薪资 select salary from emp where name ='灭绝'; -- b. 查询比她工资高的员工数据 select*from emp where salary > (select salary from emp where name ='灭绝');
-- 9. 查询比平均薪资高的员工信息 -- a. 查询员工的平均薪资 selectavg(salary) from emp; -- b. 查询比平均薪资高的员工信息 select*from emp where salary > (selectavg(salary) from emp);
-- 10. 查询低于本部门平均工资的员工信息 -- a. 查询指定部门平均薪资 selectavg(e1.salary) from emp e1 Where e1.dept_id =1; selectavg(e1.salary) from emp e1 where e1.dept_id =2; -- b. 查询低于本部门平均工资的员工信息 select*from emp e2 where e2.salary < (selectavg(e1.salary) from emp e1 where e1.dept_id = e2.dept_id );
-- 11. 查询所有的部门信息,并统计部门的员工人数 select d.id , d.name , (selectcount(*) from emp e where e.dept_id = d.id) '人数'from dept d; selectcount(*) from emp where dept_id =1;
-- 12. 查询所有学生的选课情况,展示出学生名称,学号,课程名称 -- 表 : student , course , student_course -- 连接条件:student.id = student_course.studentid , course.id = student_course.courseid select s.name , s.no , c.name from student s , student_course sc , course c where s.id = sc.studentid and sc.courseid = c.id;