2012-04-16 7 views
0

は、私は、MySQLのDB上の2つのテーブル少しトリッキーmysqlのクエリ

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

任意のアイデアが

は、私はこのような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 
+2

'SUM(comments.ratingを) 、COUNT(comments.comment_date)AS num_of_comments' –

+0

私はそれが公正だと思います。私も 'GROUP BY'を忘れてしまった。一定。 – VeeArr

1

これはそれを行う必要があります。seller_rating AS

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