0

mysql db で 2 つのテーブルを取得しました

auctions:

seller: seller id
transaction_date: unix timestamp


comments:

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

そのため、トランザクションの直前に、評価の合計である売り手の売り手の評価を計算したいと考えています。取引直前に売り手が何件のコメントをしたか知りたいです。

次のような列をさらに 2 つ取得したいと考えています。

オークション:

売り手、取引、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 に答える