2014-01-30 179 views
0

這裏是交易,我有這樣的SQL查詢笛卡爾乘積加入

SELECT * 
FROM Customers AS C, Orders AS O, Orderlines AS OL 
WHERE C.CustomerID = O.CustomerID AND O .OrderID = OL.OrderID 

這關係代數是

Σ(C.CustomerID=O.CustomerID AND O.OrderID=OL.OrderID)(Customers x Orders x Orderlines) . 

我怎樣才能「轉換」這3路笛卡爾積成加入關係?

非常感謝。

P.S請糾正我,如果我對關係代數部分錯了。

+0

啊,關係代數。這種*喜歡的*回憶 – Phil

回答

2

似乎很簡單的......

SELECT c.*, o.*, ol.* 
FROM Customers c 
INNER JOIN Orders o ON c.CustomerId = o.CustomerID 
INNER JOIN OrderLines ol ON o.OrderId = ol.OrderId 

我建議對從每一個表中選擇*雖然,尤其是考慮到重複列名。

1
SELECT * 
FROM Customers AS C 
    JOIN Orders AS O ON C.CustomerID = O.CustomerID 
    JOIN Orderlines AS OL ON O.OrderID = OL.OrderID