mysql ,根据查询结果集更新表

1. 表结构

CREATE TABLE `contest_problem` (
  `problem_id` int(11) NOT NULL DEFAULT '0' COMMENT '试题号',
  `contest_id` int(11) NOT NULL DEFAULT '0' COMMENT '考试号',
  `num` int(11) DEFAULT NULL COMMENT '正确提交人数'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin COMMENT='答题统计表' ;

CREATE TABLE `solution` (
  `solution_id` int(11) NOT NULL COMMENT '答题号',
  `problem_id` int(11) DEFAULT NULL COMMENT '试题号',
  `result` int(11) DEFAULT NULL COMMENT '答题结果',
  `contest_id` int(11) DEFAULT NULL COMMENT '考试号'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin COMMENT='答题流水表' ;

2. 问题

这个答题统计表 (contest_problem) 的 num 表示该场考试(contest_id)的试题(problem_id)的正确提交人数
需要将答题流水表(solution) 中 答题结果 为4 的数据按照 考试号汇总,并更新到contest_problem 中去

3. 解决方法

联表查询后更新数据

# update select 语句(注意:必须使用inner join)
# 语法 update a inner join (select yy from b) c on a.id =c.id set a.xx = c.yy
update contest_problem a 
inner join (
select contest_id , count(problem_id) accept  from solution where result = 4 and contest_id <> '' group by contest_id
) b 
set a.accept = b.accept where a.contest_id = b.contest_id
life is beautiful,我是Alon(阿龙),如果你有问题,欢迎给我留言。

参考

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值