//先创建两张对应的员工表
create table emp(ename varchar(11),sal int,deptno int);//员工
create table dept(dname varchar(11), deptno int);//部门
内连接
方言查询
select emp.ename,dept.dname,emp.sal
from emp,dept
where emp.deptno=dept.deptno;
//查询emp中的deptno和depy 的deptno相同的enamel,emp,sa,这是mysql的方言
标准查询
select emp.ename,dept.dname,emp.sal
from emp inner join dept
on emp.deptno=dept.deptno;
//和上面的方言查询效果一样
自然查询
select emp.ename,dept.dname,emp.sal from emp natural join dept;
//他会自动搜索这两张表相同的部分
================================================
外连接
select emp.ename,dept.dname,emp.sal
from emp left outer join dept
on emp.deptno=dept.deptno;
//外连接有一组一次,左外及左表为主,那么主表中所有记录无论漫步满足条件,都打印出来,右表部分事业null来补位
(将left改为right为外连接)
====================================================
子查询
select * from emp where sal=max(sal);//这是无法实现的,此时可以使用子查询
select * from emp where sal=(select max(sax) from emp);//注意,括号内的内容必须是单行单列
select * from emp where sal>all (select salfrom emp where deptno=10);//此时虽不是单行单列,但加了all意为sal大于括号内的所有内容,any同理