Update multiple columns from subquery

后端 未结 1 1237
半阙折子戏
半阙折子戏 2021-02-07 06:29

This type of thing has been asked a few times before, but is not quite what I am looking for. I need to SET two rows equal to different parts of a subquery.

相关标签:
1条回答
  • 2021-02-07 07:06

    You can simply join the table in a subquery that do some calculations,

    UPDATE  records a
            INNER JOIN
            (
                SELECT  blah, 
                        COUNT(*) totalCount,
                        SUM(amount) totalSum
                FROM    leads_table
                GROUP   BY blah
            ) b ON  b.blah = a.blah
    SET     a.leads = b.totalCount
            a.earnings = b.totalSum
    
    0 讨论(0)
提交回复
热议问题