-1

我使用以下查询过滤 My (TableA - fields app_id, user_id, points, tr_type, description )

select app_id, user_id,sum(case when tr_type = 'add' then points when tr_type = 'sub' then - points end) as points
from TableA
group by app_id, user_id;

结果:

app_id  user_id  points
1          1a      10
1          1b      12
1          1c      3

现在我有另一张桌子(TableB)

app_id   user_id   points  desc
1          1a      0       text
1          1b      0       tet
1          1c      0       txt

现在我需要快速替换查询来更新 TableB 点值,它是来自 TableA 的 groupby

4

1 回答 1

0

您可以将内部联接与 subqiery 一起用于按值分组

update tableB b
INNER JOIN (
    select app_id
        , user_id
        ,sum(case when tr_type = 'add' then points when tr_type = 'sub' then - points end) as points
    from TableA
    group by app_id, user_id

) t ON t.app_id = b.app_id
    AND t.user_id = b.user_id 
set b.points = t.points 
于 2021-02-16T14:48:59.873 回答