这此总结学到的sql表连接,以下便为此次数据,本文以mysql为例。
LTOMIXSQX7$~H5__AWFULEF.png MSIKTC5L{_~E7WH~A35GITF.png
一.外连接
1.左连接(left join)
select * from student left join course on course.c_id = student.s_id
# on后面接连接的条件
628969UTBSP}C7M2WVZW$27.png
2.右连接(right join)
select * from student right join course on course.c_id = student.s_id
EV$3~}GALB()NL`XU(TROCS.png
3.完全外连接(full join)
select * from student full join course on course.c_id = student.s_id
二.内连接(join或 inner join)
select * from student inner join course on course.c_id = student.s_id
![OO4JG04B]6G{{UG%R)AFM6.png
三.交叉连接(cross join)
注意,使用交叉连接时,一定要注意where条件,不然会出现笛卡尔积。
先演示一遍错误的,看一下结果。
select * from student cross join course
![JJKO]2_M@{]@T1CS}FXI0IA.png](https://img.haomeiwen.com/i12400467/031d9f0b352d6ba5.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
结果很长,我只截取了一部分,下面展示正确的。
select * from student cross join course on student.s_id = course.c_id
E1KX1VS%S3$(43C[@%W]{MK.png
由图可知,此结果和上面内连接结果一样。
四.以上变为简单的sql表连接语句,更复杂的以后熟练了补充。
网友评论