【问题标题】:Confusion in database with rating and transation数据库与评级和翻译的混淆
【发布时间】:2018-05-08 02:33:40
【问题描述】:

让我以我的方式解释问题

交易表

TransactionId、SellerId、BuyerId、ProductId、TransactionPrice、状态

评分表

RatingId、TransactionId、SellerId、BuyerId、ProductId、IsFromBuyer(bit)、Rate1、Rate2、Rate3

现在在我的应用程序中将有两个条目进入评级表。这将是每笔交易。 买家可以给卖家打分,卖家也可以给买家打分!

现在的问题是我如何识别已经给出评级的买家/卖家。

问题是我想要一个视图,它会给我该交易的数据 + ratingBy(seller/buyer)

谁能给我意见?

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

让我给你更多的解释:

我有两个视图 1) 交易 2) 评分

我想在交易页面上“查看”数据(我在上面指定),以便显示/隐藏评级按钮。

【问题讨论】:

  • "我怎样才能识别这样的买家/卖家已经给出了评级?(不要)只需在 ."RatingId, TransactionId, SellerId, BuyerId, ProductId, IsFromBuyer(位),以便买方/卖方不能添加多个记录并处理重复索引错误。好的,重新阅读您的意思是您需要将交易加入评级,以便获得交易和任何评级(如果存在)...SELECT T.*, R.* From Transation T LEFT JOIN Rating R on T.TransactionID = R.TransactionIID AND (T.SellerID = R.SellerID OR T.BuyerID = R.BuyerID) 但这取决于评级的填充方式。
  • 您的评分表结构对我来说似乎很奇怪。你能描述一下交易和评级表是如何关联的,以及是否总是填充卖家ID和买家ID?我的意思是您拥有 transactionID 并且可以派生卖方/买方 ID,那么除非您打算删除交易,否则为什么甚至需要它们;如果你这样做,你为什么要保持评级?似乎您只需要 ratingID、transactionID、isFromBuyer 和 Rate1、Rate2、Rate3。在关系型数据库中;我们尽量不重复数据(除非有业务需要),这似乎是 SellerID 和 BuyerID 在评级表中所做的;你需要吗?

标签: sql database


【解决方案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 行也是如此)

【讨论】:

  • 我想我理解您的问题并更新了我的答案,以便您的交易视图将具有 3 个 BuyerRating 值(如果可用)和 3 个 SellerRating 值(如果可用)与您的所有 TransactionTable 值位于同一行。跨度>
【解决方案2】:

如果您有 RatingID,并且想要获取交易的两个条目:

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

【讨论】:

  • 评分表可能有问题!如果不是那么?
猜你喜欢
  • 1970-01-01
  • 2010-10-31
  • 2011-03-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多