Sql 同一表上的多个联接Oracle

Sql 同一表上的多个联接Oracle,sql,oracle,Sql,Oracle,我尝试了下面的查询 SELECT Customer.*, ElectrnicItem.Product1 AS ElectronicItem1, ElectrnicItem.Product2 AS ElectronicItem2, ElectrnicItem.Product3 AS ElectronicItem3, ApparelItem.Product1 AS ApparelItem1, ApparelItem.Product

我尝试了下面的查询

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列定义不明确


您缺少将库存中的行标识为“电子”或“服装”的任何谓词。不过我不确定这是不是解决办法。

这个怎么样

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';

我创建了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
表包含
CustomerId
CustomerName
Customer.
将同时检索
CustomerId
CustomerName
。因此
CustomerName
列不能包含任何
NULL
价值观

好吧,有点晚了,但是。。。 在Oracle中,您不使用AS来为表别名,只使用AS来为列别名。尝试从表中删除所有这些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)

你能把Customer.*分解成Customer.CustomerID,Customer.CustomerName吗?不确定这是否是问题所在。Customer表中的列的名称是什么?能否用
CREATE table
语句替换图像?通常情况下,发布真实代码(我们可以复制和粘贴文本来测试自己)比发布代码图片要好。是否可以将数据标准化?