0

甲骨文 11g

PERSON 表同时包含卖家和买家 ID。如何让买家和卖家进入单一结果集?我可以获得列出买方或卖方的结果,但不能同时列出两者。

Person
=================
 PersonID First   Last
  1      Joe      Camel
  2      Ronald   McFly
  3      Barbara  Wawa

 SalesDetail
 =========================
 TransID   Amount  SellerID CustomerID
 98         500     1        2
 99         700     3        1

Desired Result
===========================================
SellerID    SellerLast  BuyerID BuyerLast  Amount
1           Camel       2       McFly      500
3           Wawa        1       Camel      700
4

2 回答 2

1

只需加入Person表两次

SELECT sd.sellerID,
       seller.last sellerLast,
       sd.buyerID,
       buyer.last buyerLast,
       sd.amount
  FROM salesDetail sd
       JOIN person seller ON (sd.sellerID = seller.personID)
       JOIN person buyer  ON (sd.buyerID  = buyer.personID)

如果买方或卖方可能未知,您可能需要外部联接。

于 2012-05-04T16:17:35.137 回答
1

试试这个

select seller.sellerid, 
       seller.last,
       buyer.buyerid,
       buyer.last, 
       amount
from
person buyer
inner join salesdetail on buyer.personid = salesdetail.cutomerid
inner join person seller on salesdetail.sellerid = seller.personid

目前无法测试自己

于 2012-05-04T16:20:38.373 回答