Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MySQL:对列中的单元格求和,并将其插入到与条件相同的表中_Mysql_Sql - Fatal编程技术网

MySQL:对列中的单元格求和,并将其插入到与条件相同的表中

MySQL:对列中的单元格求和,并将其插入到与条件相同的表中,mysql,sql,Mysql,Sql,我需要将列中的特定单元格和条件相加,并将其插入同一个表中 表结构: 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

我需要将列中的特定单元格和条件相加,并将其插入同一个表中

表结构:

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
每个用户只有一个“全部”分数(在“类型”列中)和多个“每日”分数

如何汇总“每日”分数并将其插入“全部”行

比如:


谢谢,

您正在执行的是
更新
,而不是
插入
。您可以使用
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';
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';