sql中的内连接和外连接

左连接语句,left outer join

右连接语句,right outer join

内连接语句,inner join

下面举个例子说明。

建立一个student和clazz表

clazz                                                      

id    name

1       一班

2       二班

3        三班

student           

id     name      clazz_id

1       小明            2

2       小红            3

3        小张

内连接查询:select* from student inner join clazz on clazz.id=student.clazz_id;

查询结果,两个表相同条件的的部分显示出来。

 

左连接查询:select* from student left  outer  join clazz

 on clazz.id=student.clazz_id;

student表中内容全部显示,clazz表显示符合的条件,clazz中没有的地方显示为null。

查询结果

 

右连接查询:select* from student right outer  join clazz 

on clazz.id=student.clazz_id;

clazz表中内容全部显示,student表显示符合的条件,student中没有的地方显示为null。

查询结果

 

猜你喜欢

转载自www.cnblogs.com/gnos/p/13172094.html