多表连接
作者:邹爱红 ,撰写时间:2019/4/1
笛卡尔集:
笛卡尔集省略连接条件,连接条件无效
就是简单的拼接两张表,然后从两张表中查询出来那个字段的数据,没有用连接的,这就是笛卡尔集。
Select ebiao.id, ebiao.name,dbiao.id,dbiao.name
From ebiao,dbiao
等值连接 =
Select ebiao.id, ebiao.name, dbiao.id,dbiao.name
From ebiao,dbiao
Where ebiao.dbiaoid=dbiao.dbiaoid
非等值连接 BETWEEN…AND… 就是说它们,,,之间的值
Select ebiao.name,dbiao.name, dbiao.type
From ebiao e,dbiao d
Where e.name
between d.name and d.type
外链接 是显示那个表的所有数据,右边就显示右边的那个表的所有数据,外连接分三种,左连接 left join ,right join右连接,满外连接左右一起full outer join
举个例子:查询所有学生的Sname、Cno和Degree列。
满外连接:就是显示两张表的所有数据
select stu.sname,sco.cno,sco.degree
from student
stu full outer join score sco on stu.sno =sco.sno
右连接:右边的表会全部显示出来
select stu.sname,sco.cno,sco.degree
from score sco right join student stu on stu.sno=sco.sno
左连接:就是说左边的表会全部显示
select stu.sname,sco.cno,sco.degree
from score sco left join student stu on stu.sno =sco.sn
自连接
自己连接自己 这个跟别的表没有连接关系,就是它的上级ID
Select * From ebiao e, ebiao d
where e.Manager_Id= d.Employee_Id;
叉集 cross join
相当于笛卡尔集
叉集就是不用Where这一句条件的。查询出来的表有很多数据的,一对多关系
SELECT ebiao _name, dbiao_name
FROM ebiao
cross join dbiao;
自然连接 natural Join就不用on连接条件,自动找的 ,两个表有关联的字段就会自动找到
举例子:查询所有学生的Sname、Cno和Degree列。
自然连接:
select sname,degree,cno
from student
natural join score
Using 使用
指定某个连接条件,相当于on,作用相同
使用 USING 可以在有多个列满足条件时进行选择。
–不要给选中的列中加上表名前缀或别名。
–JOIN 和 USING 子句经常同时使用。
就省略掉了on后面的什么id等于什么ID,跟on的作用一样
就是直接指定某个值