Bテーブルのデータを、Aテーブルにインサート&アップデートするSQL

INSERT INTO member_cost (
    member_id
    , cost
)
SELECT
    member_id
    , cost
FROM
(
    SELECT
        member_id
        , SUM(cost) as cost
    FROM member_daily_cost
    GROUP BY member_id
) t1
ON DUPLICATE KEY UPDATE
    member_id = t1.member_id
    , cost = t1.cost
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account log in.