SQL server多表联合查询

1. 外连接可分为:左连接、右连接、完全外连接。

a.  左连接  left join或 left outer join

SQL语句:select * from student left join course onstudent.ID=course.ID

{左外连接包含left join左表所有行,如果左表中某行在右表没有匹配,则结果中对应行右表的部分全部为空(NULL).

注:此时我们不能说结果的行数等于左表数据的行数。当然此处查询结果的行数等于左表数据的行数,因为左右两表此时为一对一关系。}

b.  右连接  right join或 right outer join

SQL语句:select * from student right join course onstudent.ID=course.ID

{右外连接包含right join右表所有行,如果左表中某行在右表没有匹配,则结果中对应左表的部分全部为空(NULL)。

注:同样此时我们不能说结果的行数等于右表的行数。当然此处查询结果的行数等于左表数据的行数,因为左右两表此时为一对一关系。}

c.  完全外连接  full join或 full outer join

SQL语句:select * from student full join course onstudent.ID=course.ID

{完全外连接包含full join左右两表中所有的行,如果右表中某行在左表中没有匹配,则结果中对应行右表的部分全部为空(NULL),如果左表中某行在右表中没有匹配,则结果中对应行左表的部分全部为空(NULL)。}


2. 内连接join 或 inner join

SQL语句:select * fromstudent inner join course on student.ID=course.ID

innerjoin 是比较运算符,只返回符合条件的行。

此时相当于:select * from student,course where student.ID=course.ID


3. 交叉连接 cross join

1.概念:没有 WHERE子句的交叉联接将产生连接所涉及的表的笛卡尔积。第一个表的行数乘以第二个表的行数等于笛卡尔积结果集的大小。

SQL语句:select * from student cross join course where student.ID=course.ID


猜你喜欢

转载自blog.csdn.net/zou15093087438/article/details/79226394