LEFT JOIN:
基本语法:
SELECT column_name(s) FROM table_name1 LEFT JOIN table_name2 ON table_name1.column_name=table_name2.column_name
左关联是以左边的表为主,即使右边的表没有匹配的记录,比如:
要列出所有的人和对应的OrderNo,因为Carter在表二中没有对应的记录,所以用左连接:
SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo FROM Persons LEFT JOIN Orders ON Persons.Id_P=Orders.Id_P ORDER BY Persons.LastName
结果是:
RIGHT JOIN:
基本语法:
SELECT column_name(s) FROM table_name1 RIGHT JOIN table_name2 ON table_name1.column_name=table_name2.column_name
和左联接类似,只是这里是以右边的表为主,即使左边的表没有匹配的记录,比如:
现在要求列出所有的OrderNo,以及对应的人:
SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo FROM Persons RIGHT JOIN Orders ON Persons.Id_P=Orders.Id_P ORDER BY Persons.LastName
结果是: