0

Possible Duplicate:
right join versus left join

is there any need of RIGHT join, bacause we can achieve the same result using LEFT join by just altering the table name

i have two tables Persons and Orders

P_Id    LastName    FirstName   Address City
1   Hansen  Ola Timoteivn 10    Sandnes
2   Svendson    Tove    Borgvn 23   Sandnes
3   Pettersen   Kari    Storgt 20   Stavanger

and

O_Id    OrderNo P_Id
1   77895   3
2   44678   3
3   22456   1
4   24562   1
5   34764   15

and i have a query for left join as:

SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo
FROM Persons
LEFT JOIN Orders
ON Persons.P_Id=Orders.P_Id
ORDER BY Persons.LastName

and with right join:

SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo
FROM Orders
RIGHT JOIN Persons
ON Orders.P_Id=Persons.P_Id
ORDER BY Persons.LastName

both gives the same result.

4

2 回答 2

1

为了方便起见,两个版本都存在。正如你所说,只有一个是必要的。

但是,有时如果只有一个(RIGHT 或 LEFT),并且您想在查询中添加非内部联接而不进行太多更改,则有时必须对大型查询进行相当多的更改。

将两者都作为一个选项,通常很容易将非内部联接加入到预先存在的查询中,而无需对查询进行太多其他修改。

至少那是我发现自己编写 RIGHT JOIN 的唯一一次:当我已经有一个查询并且需要添加一个非内部连接并使其成为 LEFT JOIN 时,我会比我想要的更多地重新编写查询。

于 2013-01-15T07:05:30.770 回答
-1

检查这个以获得更多理解

http://www.codinghorror.com/blog/2007/10/a-visual-explanation-of-sql-joins.html

于 2013-01-15T07:05:24.443 回答