2012-04-27 59 views
0

早安朋友数据库,让我用我的方式解释问题混乱与评级和TRANSATION

事务表

TRANSACTIONID,SellerId,BuyerId,产品编号,TransactionPrice,状态

RatingTable

评级Id,Transa ctionId,SellerId,BuyerId,ProductId,IsFromBuyer(bit),Rate1,Rate2,Rate3

现在在我的应用程序中会有两个输入到等级表中。这将是每笔交易。 买家可以评价卖家,因为卖家可以评价买家!

现在的问题是我如何识别这样的买方/卖方已经给出评分。

的事情是我想它会给我的交易+ ratedBy(卖方/买方)的数据

谁能给我的想法有何看法?

看!我可以简单地引发一些查询,但事情是我不确定评级表必须有记录!它可能有记录

让我给你更多的解释

我有两个观点1)交易2)评级

我想要的“视图”数据(i上面指定的)交易页面这样我就可以显示/隐藏评级按钮。

回答

1

这将取决于您正在使用的数据库供应商,但您只想在RatingTable上连接两次,并根据您的IsFromBuyer列筛选连接。

它可能看起来像这样在Oracle:

create or update view TransactionView AS 
    select TransactionTable.*, 
      BuyerRating.Rate1 BuyerRate1, BuyerRating.Rate2 BuyerRate2, BuyerRating.Rate3 BuyerRate3, 
      SellerRating.Rate1 SellerRate1, SellerRating.Rate2 SellerRate2, SellerRating.Rate3 SellerRate3 
     from TransactionTable 
    left join RatingTable BuyerRating 
      on TransactionTable.TransactionId = BuyerRating.TransactionId 
      AND BuyerRating.IsFromBuyer = true 
    left join RatingTable SellerRating 
      on TransactionTable.TransactionId = SellerRating.TransactionId 
      AND SellerRating.IsFromBuyer = false 

如果没有BuyerRating行,那么你将不得不在BuyerRate1/2/3场NULL值,因为它是一个LEFT JOIN所有TransactionTable行即使在RatingTable中没有匹配的BuyerRating行加入,也包括在内。 (同样,对于缺少SellerRating行)

+0

再次参考的问题。我编辑了 – Chintan 2012-04-27 05:21:26

+0

我认为我理解您的问题并更新了我的答案,以便您的交易视图具有3个买方评级值(如果可用)以及3个卖家评级值(如果可用于与所有交易表值相同的行)。 – nvuono 2012-04-27 05:56:39

0

如果你有RatingID,和你想获得交易两个条目:

SELECT * FROM TransactionTable x 
INNER JOIN RatingTable y 
ON x.TransactionID = y.TransactionID 
WHERE TransactionID in (SELECT TransactionID from RatingTable where RatingID = 1234) 
+0

评级表可能有antry!如果不是呢? – Chintan 2012-04-27 05:22:54