sql之left join、right join、inner join的区?
left join(左联? 返回包括左表??有?录和右表?结字段相等的记录
right join(右联? 返回包括右表??有?录和左表?结字段相等的记录
inner 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
(所影响的?数为 5 行)
结果说明:
left join?A表的记录为基??A?看成左表,B?看成右表,left join?左表为准?
换句话?,左表(A)的?录将会全部表示出?而右?B)?显示符合搜索条件的??例子?: A.aID = B.bID).
B表?录不足的地方均为NULL.
--------------------------------------------
2.right join
sql?如下:
select * from A
right 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
NULL?????NULL?????8?????2006032408
(所影响的?数为 5 行)
结果说明:
仔细观察??就会发现,和left join的结果刚好相?这??右表(B)为基??A表不足的地方用NULL?.
--------------------------------------------
3.inner join
sql?如下:
select * from A
innerjoin 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
结果说明:
很明?这里?示出?A.aID = B.bID的??这?明inner join并不以谁为基?,它只显示符合条件的??
--------------------------------------------
?
LEFT JOIN操作用于在任何的 FROM 子句?组合来源表的记录。使?LEFT JOIN 运算来创建一?边?部联接?左边?部联接将包含了从??左边)开始的两个表中的全部?录,即使在?二个(右边)表中并没有相符?的记录?
?:FROM table1 LEFT JOIN table2 ON table1.field1 compopr table2.field2
说明:table1, table2参数用于指定要将记录组合的表的名称??
field1, field2参数指定?接的字?的名称?且这些字?必须有相同的数据类型及包?同类型的数据,但它们不需要有相同的名称??
compopr参数指定关系比较运算符:"="?"<"?">"?"<="?">=" ?"<>"?
如果在INNER JOIN操作??联接包含Memo 数据类型?OLE Object 数据类型数据的字段,将会发生错?.