好的,需要帮助!
表格轮次
id|blobs|total
表格数据
id|roundid|score
数据表中有18个角色,roundid与圆桌的id有关。
我试图做这样的事情:
update rounds set blobs=count(*) where score=-1
update rounds set total = sum(score) where score != -1
基本上找一个查询来调用得分= -1的总计数,以及与该回合有关的总得分!= -1。
可能,还是我需要浏览每一轮?
答案 0 :(得分:1)
使用子查询获取所需的值:
UPDATE `rounds` r
INNER JOIN (
SELECT d.roundid,
SUM(CASE WHEN d.score = -1 THEN 1 ELSE 0 END) AS blobs,
SUM(CASE WHEN d.score != -1 THEN d.score ELSE 0 END) AS total
FROM `data` d
GROUP BY d.roundid
) T1 on (T1.roundid = r.id)
SET r.blobs = T1.blobs,
r.total = T1.total