outer join , inner join , cross join

select * from table1;

ID NAME
1 wang
2 liu
3 yang

select * from table2;

ID SCORE

2 80
3 95
4 90
----------------------------------
一,外连接
1.左外连接(left join 或 left outer join)
左向外联接的结果集包括 LEFT OUTER 子句中指定的左表的所有行,而不仅仅是联接列所匹配的行。如果左表的某行在右表中没有匹配行,则在相关联的结果集行中右表的所有选择列表列均为空值(null)。
select *
from table1  left outer join table2 
on table1.id=table2.id


ID NAME ID_1 SCORE
2 liu 2 80
3 yang 3 95
1 wang

2.右外连接
select *
from table1  t1  right outer join table2  t2
on t1.id=t2.id

ID NAME ID_1 SCORE

2 liu 2 80
3 yang 3 95
4 90

3.完整外部联接:full join 或 full outer join
完整外部联接返回左表和右表中的所有行。当某行在另一个表中没有匹配行时,则另一个表的选择列表列包含空值。如果表之间有匹配行,则整个结果集行包含基表的数据值。
select *
from table1  t1  full outer  join table2  t2
on t1.id=t2.id

ID NAME ID_1 SCORE

2 liu 2 80
3 yang 3 95
1 wang
4 90

二,内连接
select *
from table1  t1  inner  join table2  t2
on t1.id=t2.id
=
select *
from table1 t1 cross join table2 t2
where t1.id=t2.id;
(注:cross join后加条件只能用where,不能用on)


ID NAME ID_1 SCORE

2 liu 2 80
3 yang 3 95

三,交叉连接
  没有 WHERE 子句的交叉联接将产生联接所涉及的表的笛卡尔积。第一个表的行数乘以第二个表的行数等于笛卡尔积结果集的大小。(table1和table2交叉连接产生3*3=9条记录)
select *
from table1 t1 cross join table2 t2
=
select *
from table1,table2

ID NAME ID_1 SCORE

1 wang 2 80
1 wang 3 95
1 wang 4 90
2 liu 2 80
2 liu 3 95
2 liu 4 90
3 yang 2 80
3 yang 3 95
3 yang 4 90

你可能感兴趣的:(Inner Join)