通俗易懂的joinleft joinright joinfull joincross join
Posted duancf
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了通俗易懂的joinleft joinright joinfull joincross join相关的知识,希望对你有一定的参考价值。
内连接:列出与连接条件匹配的数据行(join\\inner join)
外连接:两表合并,如有不相同的列,另外一个表显示null(left join\\right join\\full outer join\\cross join)
以学生表:#table1和课程表:#table2为例
select * from #table1 stu join #table2 cou on stu.student_id = cou.stu_id;
Inner join 等于join(当条件都满足时,列出满足条件的项)
执行结果和用where相同()
select * from #table1 stu left join #table2 cou on stu.student_id = cou.stu_id;
left join :以左表为主,右表没有的项显示null;
select * from #table1 stu right join #table2 cou on stu.student_id = cou.stu_id;
right join:以右表为主,左表没有的项显示null;
select * from #table1 stu full outer join #table2 cou on stu.student_id = cou.stu_id;
full outer join : 没有主次表之分,显示所有,相当于左、右连接的组合;
在Sql server数据库中,full join 和 full outer join(其他数据库没试过)
select * from #table1 stu full join #table2 cou on stu.student_id = cou.stu_id; ——结果和full outer join 一样;
select * from #table1 cross join #table2 ;
cross join(交叉连接\\笛卡儿积):返回左表中的所有行,左表中的每一行与右表中的所有行组合;
以上是关于通俗易懂的joinleft joinright joinfull joincross join的主要内容,如果未能解决你的问题,请参考以下文章
SQL表连接查询(inner joinfull joinleft joinright join)
SQL中inner joinleft joinright joinouter join之间的区别
SQL表连接查询(inner joinfull joinleft joinright join)
SQL表连接查询(inner joinfull joinleft joinright join)