首页 > 代码库 > MySQL update时使用联表,聚合

MySQL update时使用联表,聚合

原文地址 http://stackoverflow.com/questions/3022405/mysql-update-query-with-left-join-and-group-by

 

UPDATE
    Table AS t
    LEFT JOIN (
        SELECT
            Index1,
            Index2,
            COUNT(EventType) AS NumEvents
        FROM
            MEvents
        WHERE
            EventType = A OR EventType = B
        GROUP BY
            Index1,
            Index2
    ) AS m ON
        m.Index1 = t.Index1 AND
        m.Index2 = t.Index2
SET
    t.SpecialEventCount = m.NumEvents
WHERE
    t.SpecialEventCount IS NULL

 

MySQL update时使用联表,聚合