【发布时间】:2021-05-19 10:07:14
【问题描述】:
我使用以下查询过滤我的(TableA - 字段 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
【问题讨论】:
-
如果您可以查询一个数字,最好这样做,而不是将总数保存在表格中,因为它们几乎总是错误的。某个地方的某个人会快速修复数据库并忘记维护总数
标签: mysql group-by sql-update mysqlupgrade