多表查询
一、sql92格式
-
内连接
*等值连接:
where table1.key = table2.id
*非等值连接:
where table1.salary between t2.low and t2.high
;//不再是等于判断*自连接 :
where t1.managerId = t1.id
;//在一个表中连接两个字段
二、sql99格式
语法格式:select 查询列表 from 表1 别名 **【连接类型】 join 表2 别名 on 连接条件** 【where条件...】
内连接仅显示两表匹配数据,外连接 = 内连接 + 未匹配NULL数据,左外连接以左表为主,右连接以右表为主
-
内连接:inner
*等值连接:
inner join table2 on table1.key = table2.id
*非等值连接:
inner join t2 on t1.salary between t2.low and t2.high
;//不再是等于判断*自连接:
inner join t1 on t1.managerId = t1.id
;//在一个表中连接两个字段 -
外连接:outer
*左外连接:left outer
select * from t1 left join t2 on t1.id = t2.UID;
*右外连接:right outer
select * from t1 right join t2 on t1.id = t2.UID;
-
交叉连接:cross
select * from t1 right join t2
;//笛卡尔乘积结果
为表起别名,可以提高语句的简介度,关键词as可以省略