2013-05-03 123 views
1

enter image description here多加入同一個表,甲骨文

我想下面的查詢

SELECT Customer.*, 
     ElectrnicItem.Product1 AS ElectronicItem1, 
     ElectrnicItem.Product2 AS ElectronicItem2, 
     ElectrnicItem.Product3 AS ElectronicItem3, 
     ApparelItem.Product1 AS ApparelItem1, 
     ApparelItem.Product2 AS ApparelItem2, 
     ApparelItem.Product3 AS ApparelItem3 
FROM Customer 
LEFT JOIN Inventory AS ElectrnicItem 
ON (Customer.CustomerID = ElectrnicItem.CustomerID) 
LEFT JOIN Inventory AS ApparelItem 
ON (Customer.CustomerID = ApparelItem.CustomerID) 

但它總是返回:含糊不清的

ORA-00918列

+0

您是否可以將Customer. *分解爲Customer.CustomerID,Customer.CustomerName?不知道這是否是問題。 – Hazaart 2013-05-03 09:09:19

+0

客戶表中的列名稱是什麼? – Nagasaki 2013-05-03 09:10:52

+0

你可以用'CREATE TABLE'語句替換圖像嗎?發佈實際代碼(我們可以複製和粘貼以測試自己的文本)通常比代碼圖片更好。 – 2013-05-03 09:12:37

回答

0

您錯過了任何謂詞來標識清單中的行爲'Elec tronic「或」服裝「。不知道這是修復。

0

這個怎麼樣?

SELECT a.customer_id, a.customer_name, b.product1 electronicitem1, 
     b.product2 electronicitem2, b.product3 electronicitem3, 
     c.product1 apparelitem1, c.product2 apparelitem2, 
     c.product3 apparelitem3 
FROM customer a, inventory b, inventory c 
WHERE a.customer_id = b.customer_id(+) 
    AND b.product_type(+) = 'Electronic' 
    AND a.customer_id = c.customer_id(+) 
    AND c.product_type(+) = 'Apparel'; 
0

我創建了表Customer和Inventory,還插入了您提供的值。以下是查詢:

select customer.*,ElectrnicItem.Product1 as ElectronicItem1, 
ElectrnicItem.Product2 as ElectronicItem2, 
ElectrnicItem.Product3 as ElectronicItem3, 
ApparelItem.Product1 as ApparelItem1, 
ApparelItem.Product2 as ApparelItem2, 
ApparelItem.Product3 as ApparelItem3 
from customer 
left join inventory as ElectrnicItem on 
(customer.CustomerID = ElectrnicItem.CustomerID) 
left join inventory as ApparelItem 
on (customer.CustomerID = ApparelItem.CustomerID) 

它工作正常,並給出以下結果:

1 David Miller mobile headphone trimmer mobile headphone trimmer 
    2 Johnson   jeans tshirt  NULL jeans tshirt  NULL    
    3 Diggs   NULL NULL  NULL NULL NULL  NULL    

你提到的預計產量將永遠無法實現。由於Customer表包含CustomerIdCustomerName,Customer.*將檢索CustomerIdCustomerName。因此CustomerName列不能包含任何NULL 值。

0

好吧,有點晚了,但是... 您不能使用AS來爲Oracle中的表格別名,只能爲列別名。嘗試從表別名中刪除所有這些AS:

SELECT Customer.*, 
     ElectrnicItem.Product1 AS ElectronicItem1, 
     ElectrnicItem.Product2 AS ElectronicItem2, 
     ElectrnicItem.Product3 AS ElectronicItem3, 
     ApparelItem.Product1 AS ApparelItem1, 
     ApparelItem.Product2 AS ApparelItem2, 
     ApparelItem.Product3 AS ApparelItem3 
FROM Customer 
LEFT JOIN Inventory ElectrnicItem 
ON (Customer.CustomerID = ElectrnicItem.CustomerID) 
LEFT JOIN Inventory ApparelItem 
ON (Customer.CustomerID = ApparelItem.CustomerID)