2016-11-09 120 views
0

我剛剛開始SQL學習,並在網上做一些練習題。 我遇到了這個問題,在這裏(#11頁):http://sqlzoo.net/wiki/AdventureWorks_hard_questionsSQL:如何根據條件從同一數據源創建兩列?

下面是我們將使用這個問題的3代表的模式:

CustomerAW(客戶ID,名字,中間名,姓氏,公司名稱,EmailAddress的)

CustomerAddress(客戶ID,AddressID,地址類型)

地址(添加ressID,AddressLine1,AddressLine2,市,StateProvince,CountyRegion,POSTALCODE)

,問題是:

對於在達拉斯 '總公司' 每一位客戶顯示 '主辦公室' 和AddressLine1的的AddressLine1 '送貨'地址 - 如果沒有送貨地址,請將其留空。每個客戶使用一行。

下面是我的代碼,這樣做:

SELECT CustomerID, AddressType, AddressLine1 
FROM CustomerAW 
JOIN CustomerAddress 
USING (CustomerID) 
JOIN Address 
USING (AddressID) 
WHERE AddressType IN 'Main Office' 
AND City = 'Dallas' 
; 

下面是輸出我得到:

Result: 
CustomerID AddressType AddressLine1 
112   Main Office P.O. Box 6256916 
130   Main Office Po Box 8259024 
165   Main Office 2500 North Stemmons Freeway 
201   Main Office Po Box 8035996 
256   Main Office 99828 Routh Street, Suite 825. 

但我不知道如何顯示在一個單獨的列送貨地址。 ,以便標題變爲 「CustomerID,AddressType,MainOfficeAddress,ShippingAddress」。如問題所述,如果沒有送貨地址,請將其留空。

更新

如何做到這一點,而不使用 「查看」?網站我在沒有讓我創建視圖測試...

我曾嘗試

嗯,我已經嘗試了不同的方式來實現這一目標,其中之一是寫在子查詢SELECT語句:

SELECT c.CustomerID, 
(
SELECT a.AddressLine1 FROM Address a 
JOIN CustomerAddress ca 
USING (AddressID) 
WHERE ca.AddressType = 'Main Office' 
) AS MainOfficeAddress 
FROM CustomerAW c 
; 

但是然後我會看到「子查詢返回多於一行」的錯誤。

我也試過類似下面(他們沒有實際工作):

SELECT c.CustomerID, b.AddressLine1, e.AddressLine1 
FROM CustomerAW c 
JOIN 
(
SELECT * FROM CustomerAddress ca 
JOIN Address a 
USING (AddressID) 
WHERE ca.AddressType IN 'Main Office' 
) AS b 
USING (CustomerID) 
JOIN 
(
SELECT * FROM CustomerAddress ca 
JOIN Address a 
USING (AddressID) 
WHERE ca.AddressType IN 'Shipping' 
) AS e 
USING (CustomerID) 
; 

和:

SELECT c.CustomerID, AddressType, AddressLine1 AS MainOfficeAddress, 
(
    SELECT a.AddressLine1 FROM Address a 
    JOIN CustomerAddress ca USING (AddressID) 
    WHERE ca.CustomerID IN c.CustomerID 
    AND ca.AddressType = 'Shipping' 
) AS ShippingAddress 
FROM CustomerAW c 
JOIN CustomerAddress ca 
USING (CustomerID) 
JOIN Address 
USING (AddressID) 
WHERE ca.AddressType IN ('Main Office') 
AND City = 'Dallas' 
; 

任何幫助,不勝感激!希望我不是在問一個愚蠢的問題。

回答

1

您應該使用'內部連接'。 寫相同的代碼如上述,創建用於簡單的圖,我的意思是

create view ee as 
(
    SELECT CustomerID, AddressType, AddressLine1 
    FROM CustomerAW 
    JOIN CustomerAddress 
    USING (CustomerID) 
    JOIN Address 
    USING (AddressID) 
    WHERE AddressType IN 'Main Office' 
    AND City = 'Dallas' 
) ; 

然後寫..

create view rr as 
    (
    SELECT CustomerID, AddressType, AddressLine1 
    FROM CustomerAW 
    JOIN CustomerAddress 
    USING (CustomerID) 
    JOIN Address 
    USING (AddressID) 
    WHERE AddressType IN 'Shipping Address' 
    ); 

然後乘坐意見EE和RR

select CustomerID, AddressType, MainOfficeAddress, ShippingAddress 
from ee 
inner join 
rr 
on ee.customerID=rr.CustomerID 

希望這有助於內加入....;)

+0

嗨Tanuj,非常感謝你的回答!我可以問一下,如何使用「視圖」來實現這個功能?另外...第一次聽到使用「視圖」,你可以擴大一點看法是什麼?我認爲視圖更適合你「查看」表格,而不是用於查詢...... – alwaysaskingquestions

+0

視圖是一個虛擬表格,它指的是一個真正的表格。它存儲一些數據。一旦聲明瞭視圖,就可以像使用表一樣使用視圖。 –

+0

我明白了。因此無法創建新表或使用子查詢,而是在此處使用視圖。並且視圖只能暫時存儲在內存中,並且在您下次嘗試拉取視圖時將不可用。我的理解是否正確? – alwaysaskingquestions

相關問題