连接查询

 

 

 包括:

内连接(inner join)

外连接:

     左外连接(left join)

     右外链接(right join)

part1:内连接(inner join)

语法:

  select  要查询内容

  from 表名1

  inner join 表名2

  on 条件

解析:  

先引入一个概念

select  要查询内容

from  表1 ,表2

可以查询两个表中的数据,但是数据是交叉查询

如:

select Student.StudentNo,Address_IS.SName
from Student,Address_IS
运行结果

 加上限制条件

where student.StudentNo = Address_IS.Id,运行结果

所以两个表进行查询要加where,而内连接(inner join),从某种意义上说

select  列名                                            select   列名

from 表名1,表名2       等价于              from 表名1

where    ...                                             inner join  表名2

                                                              on    ...     

也有不一样的地方,左边的表直接查询表一表二,右边的表查询表1关联表2

同样三表内连接:

select 列名

from 表名1

inner join  表2  on ...

inner join 表3   on...

左外连接:

内连接和外连接不一样的地方,就是外连接需要主表,而内连接不需要

select  列名

from  表名1

left join  表名2 

on   ...

左外连接主表在left join 的左边,右外连接主表在 right join的右边

主表起的作用是,主表内的内容会被全部输出,不会省掉null值的行。其他表与主表匹配,返回到结果集。

右外连接:

语法:

select  列名

from  表名1

right  join  表名2

on   ....

此时右表为主表,右表逐条去匹配记录;否则NULL填充

 

posted @ 2020-04-21 20:54  多多🙂nice  阅读(293)  评论(0编辑  收藏  举报