我需要将条件中的特定单元格与条件相加并将其插入到同一个表格中
表格结构:
id user_id type scores
1 1 daily 10
2 1 daily 20
3 1 all 500
4 1 daily 5
5 2 all 200
6 3 all 300
7 2 daily 23
8 1 cat 11
9 2 daily 25
10 3 daily 30
每个用户只有一个“全部”分数(在“类型”列中)和多个“每日”分数,
如何将“每日”分数相加并将其插入“所有”行
像:
id user_id type scores
1 1 daily 10
2 1 daily 20
3 1 all 35
4 1 daily 5
5 2 all 48
6 3 all 30
7 2 daily 23
8 1 cat 11
9 2 daily 25
10 3 daily 30
感谢,
答案 0 :(得分:2)
您正在进行update
,而不是insert
。您可以使用join
:
update structure s join
(select user_id, sum(scores) as sums
from structure s
where type = 'daily'
group by user_id
) ss
on s.user_id = ss.user_id
set s.scores = ss.sums
where s.type = 'all';