[英]Have any MySQL query to Update/replace Group By table points into other MySQL Table
我使用以下查詢過濾 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
您可以將內部聯接與 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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.