先创建两个表,一个学生表,一个成绩表。
一、内连接
概念:两个连接表的匹配行的连接
关键语句:table1 inner join table2 on
例子:
select * from Student inner join Score on Score.s_id=Student.s_id
这是进行内连接之后的表,展现出来的是共同的数据 (只返回匹配的行)
二、外连接(包括:左外连接,右外连接)
1) 左连接(左外连接)
概念:将返回右表的所有行。如果左表的某行在右表中没有匹配行,则将为右表返回空值
关键语句:table1 left (outer) join table2 on
例子:
select * from Student LEFT JOIN Score ON Student.s_id=Score.s_id
select * from Student left outer JOIN Score ON Student.s_id=Score.s_id
以左表为主表,右表没数据为null
2) 右连接(右外连接)
概念:将返回右表的所有行。如果右表的某行在左表中没有匹配行,则将为左表返回空值;
关键语句:table1 right (outer) join table2 on
例子:
select * from Student right JOIN Score ON Student.s_id=Score.s_id
select * from Student right outer JOIN Score ON Student.s_id=Score.s_i
以右表为主表,左表中没数据的为null
三、交叉连接(笛卡尔积)
概念:不带WHERE条件子句,它将会返回被连接的两个表的笛卡尔积,返回结果的行数等于两个表行数的乘积(例如:T_student和T_class,返回4*4=16条记录),如果带where,返回或显示的是匹配的行数。
关键语句:table1 cross join table2
例子:
select * from Student cross JOIN Score