多表查询(续)
连接查询
- 内连接:
相当于查询集合A与集合B的交集部分 - 外连接
- 左外连接:查询左表所有数据,以及两张表交集部分数据
- 右外连接:查询右表所有数据,以及两张表交集部分数据
- 自连接:当前表与自身的连接查询,自连接必须使用表别名
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;
select e.name, d.name from emp e inner join dept d on e.dept_id = d.id;
select e.*, d.name from emp e left outer join dept d on e.dept_id = d.id;
select d.*, e.* from emp e right outer join dept d on e.dept_id = d.id;
select d.*, e.* from dept d left outer join emp e on e.dept_id = d.id;
select a.name, b.name from emp a, emp b where a.managerid = b.id;
select a.name '员工', b.name '领导' from emp a left join emp b on a.managerid = b.id;
select * from emp where salary < 5000
union all
select * from emp where age > 50;
select * from emp where salary < 5000
union
select * from emp where age > 50;
子查询
SQL语句中嵌套SELECT语句,称为嵌套查询,又称为子查询。
子查询外部的语句可以是INSERT/UPDATE/DELETE/SELECT的任何一个,根据子查询结果不同,分为:
- 标量子查询(子查询结果为单个值)
- 列子查询(子查询结果为一列)
- 行子查询(子查询结果为一行)
- 表子查询(子查询结果为多行多列)
select id from dept where name = '销售部';
select * from emp where dept_id = 4;
select * from emp where dept_id = (select id from dept where name = '销售部');
select entrydate from emp where name = '方东白';
select * from emp where entrydate > '2009-02-12';
select * from emp where entrydate > (select entrydate from emp where name = '方东白');
select id from dept where name = '销售部' or name = '市场部';
select * from emp where dept_id in(2, 4);
select * from emp where dept_id in(select id from dept where name = '销售部' or name = '市场部');
select id from dept where name = '财务部';
select salary from emp where dept_id = 3;
select salary from emp where dept_id = (select id from dept where name = '财务部');
select * from emp where salary > all (select salary from emp where dept_id = (select id from dept where name = '财务部'));
select id from dept where name = '研发部';
select salary from emp where dept_id = (select id from dept where name = '研发部');
select * from emp where salary > any (select salary from emp where dept_id = (select id from dept where name = '研发部'));
select * from emp where salary > some (select salary from emp where dept_id = (select id from dept where name = '研发部'));
select salary, managerid from emp where name = '张无忌';
select * from emp where salary = 12500 and managerid = 1;
select * from emp where (salary, managerid) = (12500, 1);
select * from emp where (salary, managerid) = (select salary, managerid from emp where name = '张无忌');
select job, salary from emp where name = '鹿杖客' or name = '宋远桥';
select * from emp where (job, salary) in (select job, salary from emp where name = '鹿杖客' or name = '宋远桥');
select * from emp where entrydate > '2006-01-01';
select e.*, d.* from (select * from emp where entrydate > '2006-01-01') e left join dept d on e.dept_id = d.id;
create table salgrade(
grade int,
losal int,
hisal int
) comment '薪资等级表';
insert into salgrade values (1, 0, 3000);
insert into salgrade values (2, 3001, 5000);
insert into salgrade values (3, 5001, 8000);
insert into salgrade values (4, 8001, 10000);
insert into salgrade values (5, 10001, 15000);
insert into salgrade values (6, 15001, 20000);
insert into salgrade values (7, 20001, 25000);
insert into salgrade values (8, 25001, 30000);
select e.name, e.age, e.job, d.name from emp e, dept d where e.dept_id = d.id;
select e.name, e.age, e.job, d.name from emp e inner join dept d on e.dept_id = d.id where e.age < 30;
select distinct d.id, d.name from emp e, dept d where e.dept_id = d.id;
select e.*, d.name from emp e left join dept d on d.id = e.dept_id where e.age > 40;
select e.*, s.grade, s.losal, s.hisal from emp e, salgrade s where e.salary >= s.losal 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;
select e.*, s.grade
from emp e,
dept d,
salgrade s
where e.dept_id = d.id
and (e.salary between s.losal and hisal)
and d.name = '研发部';
select avg(e.salary) from emp e, dept d where e.dept_id = d.id and d.name = '研发部';
select salary from emp where name = '灭绝';
select * from emp where salary > (select salary from emp where name = '灭绝');
select avg(salary) from emp;
select * from emp where salary > (select avg(salary) from emp);
select avg(salary) from emp where emp.dept_id = 1;
select *, (select avg(salary) from emp e1 where e1.dept_id = e2.dept_id) '平均'
from emp e2
where e2.salary < (select avg(salary) from emp e1 where e1.dept_id = e2.dept_id);
select id,name from dept;
select id,name, (select count(*) from emp e where e.dept_id = d.id) '人数' from dept d;
select count(*) from emp where dept_id = 1;
select s.name, s.no, c.name
from student s,
student_course sc,
ccourse c
where s.id = sc.studentid
and c.id = sc.courseid;
|