0

次の表を検討してください

users                                tweets
---------------------------------    ---------------------------
user_id num_retweets sum_retweets    tweet_id user_id retweeted
---------------------------------    ---------------------------
1                                    1        1       3
2                                    2        1       0
3                                    3        1       4
                                     4        2       0
                                     5        2       0
                                     6        3       1
                                     7        3       2
                                     8        3       0

カウントしたいnum_retweets: ユーザーが書いたリツイートがリツイートされたsum_retweets回数 : すべてのユーザーのツイートがリツイートされた回数。クエリの後に期待されるusersテーブルは次のとおりです。UPDATE

users
---------------------------------
user_id num_retweets sum_retweets
---------------------------------
1       2            7 <-- 3 + 4
2       0            0
3       2            3 <-- 1 + 2

これら 2 つのクエリを作成する際の助けをいただければ幸いです :-)UPDATEテーブル間で s を実行する際に問題が発生し続けています。

4

1 に答える 1

1
UPDATE
USERS u
JOIN (
SELECT 
    tweets.user_id,
    COUNT(IF(tweets.retweeted > 0, 1, null)) as num_retweets,
    SUM(tweets.retweeted) as sum_retweets
FROM tweets
GROUP BY tweets.user_id
) as t ON t.user_id = u.user_id
SET u.num_retweets = t.num_retweets, u.sum_retweets = t.sum_retweets
于 2012-07-25T19:30:52.960 に答える