2014-03-25 150 views
0

我需要創建一條SQL語句,它將從多個表中獲取以下信息: ISBN,Quantity,OrderTotal,FirstName,LastName,PhoneNumber和StreetAddress。MS-Access中的子查詢

這裏有兩個查詢我到目前爲止,給我我需要的信息:

CustomerInfo

SELECT c.CustomerID, c.FirstName, c.LastName, c.PhoneNumber, a.StreetAddress 
FROM Customers AS c, Address AS a 
WHERE c.CustomerID = a.CustomerID; 

OrderInfo的

SELECT o.CustomerID, o.OrderTotal, i.ISBN, i.Quantity 
FROM Orders AS o, OrderItems as i 
WHERE o.OrderID = i.OrderID; 

CombinedQuery釷是不行的!

SELECT c.FirstName, c.LastName, c.PhoneNumber, c.StreetAddress, o.OrderTotal, o.ISBN, o.Quantity 
FROM CustomerInfo as c, OrderInfo as o 
WHERE c.CustomerID = o.CustomerID; 

如何加入前兩個查詢到一個查詢,將提供相同的信息,第三查詢提供?

+0

什麼「不起作用」? –

回答

1

OrderInfo中沒有CustomerId字段。你需要所有三個表

SELECT c.FirstName, c.LastName, c.PhoneNumber, c.StreetAddress, 
     oi.OrderTotal, oi.ISBN, oi.Quantity 
FROM (Customers as c INNER JOIN 
     Orders AS o 
     ON c.CustomerID = o.CustomerID 
    ) INNER JOIN 
    OrderItems as oi 
    ON o.OrderID = oi.OrderID; 

你還需要學會正確的語法joinon條款。 where子句中的隱式聯接已過時,表達性較差,並且更容易出錯。

0
SELECT c.CustomerID, c.FirstName, c.LastName, c.PhoneNumber, 
a.StreetAddress, 
o.CustomerID, o.OrderTotal, 
i.ISBN, i.Quantity 
FROM Customers AS c, Address AS a, Orders AS o, OrderItems as i 
WHERE o.OrderID = i.OrderID; 
AND c.CustomerID = a.CustomerID 
AND c.CustomerID = o.CustomerID; 

嘗試作爲一個大的查詢,而不是做兩個,然後將它們組合。這將把你所有的四張桌子結合在一起。