0

我在 mysql 数据库上有 2 个表

auctions:

seller: seller id
transaction_date: unix timestamp


comments:

seller: seller id
rating (-1, 0, 1)
comment_date: unix timestamp

所以,我想在交易之前计算卖家的卖家评级,这是评级的总和。我想知道卖家在交易前有多少评论。

我想再获得两列这样的:

拍卖:

卖家,交易,seller_rating,num_of_comments

有任何想法吗?提前致谢。

4

2 回答 2

3

像下面这样的东西可以工作。基本上,您构建一个表,列出每个事务的所有相关评论,然后从那里执行计数:

SELECT auctions.seller, auctions.transaction_date, SUM(comments.rating) AS seller_rating, COUNT(comments.comment_date) AS num_of_comments
  FROM auctions
  LEFT OUTER JOIN comments 
    ON auctions.seller = comments.seller
  WHERE auctions.transaction_date > comments.comment_date
GROUP BY auctions.seller, auctions.transaction_date
于 2012-04-16T17:57:35.393 回答
1

这应该这样做:

SELECT
  a.seller,
  a.transaction_date,
  SUM(c.rating),
  COUNT(*)
FROM auctions a 
LEFT JOIN comments c on c.seller = a.seller AND c.comment_date <= a.transaction_date
GROUP BY a.seller
于 2012-04-16T17:58:59.770 回答