join,left join,inner join,full join的区别?

left join(左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录
right join(右联接) 返回包括右表中的所有记录和左表中联结字段相等的记录
inner join(等值连接) 只返回两个表中联结字段相等的行,等于join
full join,在两张表进行连接查询时,返回左表和右表中所有没有匹配的行;即查询结果是left join和right join的并集

表A记录如下:
aID     aNum
1     a20050111
2     a20050112
3     a20050113
4     a20050114
5     a20050115

表B记录如下:
bID     bName
1     2006032401
2     2006032402
3     2006032403
4     2006032404
8     2006032408

--------------------------------------------
1.left join
sql语句如下: 
select * from A
left join B 
on A.aID = B.bID

结果如下:
aID     aNum     bID     bName
1     a20050111    1     2006032401
2     a20050112    2     2006032402
3     a20050113    3     2006032403
4     a20050114    4     2006032404
5     a20050115    NULL     NULL

3.inner join
sql语句如下: 
select * from A
inner join B 
on A.aID = B.bID

结果如下:
aID     aNum     bID     bName
1     a20050111    1     2006032401
2     a20050112    2     2006032402
3     a20050113    3     2006032403
4     a20050114    4     2006032404

4.full join
sql语句如下: 
select * from A
full join B 
on A.aID = B.bID

结果如下:

aID     aNum     bID     bName
1     a20050111    1     2006032401
2     a20050112    2     2006032402
3     a20050113    3     2006032403
4     a20050114    4     2006032404
5     a20050115    NULL     NULL
NULL   NULL            8     2006032408

笛卡尔积和inner join结果相同,但是inner join效率快一点,join 默认就是 inner join

上一篇:使用c#对windows进行关机、重启或注销


下一篇:canvas缓动2