一、内连接:
1、内连接:俗称左右拼接连接;
2、内连接特点:满足连接条件的才会出现在结果里面;
内连接语法结构:
1、select 查询字段 from表1[inner] join 表2 on表1.关系字段=表2.关系字段;(等值连接)
2、select 查询字段 from 表1[inner] join 表2 on 表1.关系字段=表2.关系字段;
也可写成where条件形式:
3、select 查询字段 from 表1,表2 where 表1.关系字段=表2.关系字段;
注意点:
(1)内连接的表必须是要有关系的表(即具有相同意义字段的表)
(2)当查询的字段同时出现在连接的表当中,必须明确说明是那个表里的字段,不然会出现错误;
二、内连接也可多张表进行连接:
例:将student表和sq表以及course表内连接,查询sno,sname,sdept,cno,cname字段;
select a.sno,sname,sdept,b.cno,cname from
student a,sq b,course c
where a.sno=b.sno and b.cno=c.cno;
三、外连接:
外连接分为三种:左外连接,右外连接,全外连接。对应SQL:LEFT/RIGHT/FULL OUTER JOIN。
左连接:
select*from aaa left join bbb on aaa.C=bbb.C and aaa.D=bbb.D and aaa.E=bbb.E;
右链接:
select*from aaa right join bbb on aaa.C=bbb.C and aaa.D=bbb.D and aaa.E=bbb.E;
全连接:
select*from aaa left join bbb on aaa.C=bbb.C and aaa.D=bbb.D and aaa.E=bbb.E
union
select*from aaa right join bbb on aaa.C=bbb.C and aaa.D=bbb.D and aaa.E=bbb.E
版权声明:本文为weixin_43657184原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。