USE jtwitter;
ALTER TABLE users ADD COLUMN givenFavorites INTEGER NOT NULL DEFAULT 0 AFTER replies;
ALTER TABLE statuses ADD COLUMN givenFavorites INTEGER NOT NULL DEFAULT 0 AFTER remove;
UPDATE
    users AS a
SET
    a.givenFavorites = (
        SELECT
            COUNT(c.senderId)
        FROM
            favorites AS b,
            statuses AS c
        WHERE
            b.statusId=c.statusId AND
            c.senderId=a.userId
        GROUP BY c.senderId
    );
UPDATE
    statuses AS a
SET
    a.givenFavorites = (
        SELECT
            COUNT(b.statusId)
        FROM
            favorites AS b
        WHERE
            a.statusId=b.statusId
        GROUP BY b.statusId
    );
