使用结果集更新角色杀戮和死亡

时间:2013-04-24 20:45:41

标签: mysql

我试图根据其他问题找到答案,但我无法将逻辑付诸实践。

我可以轻松获取所需的信息,但是我无法使用这些信息更新表格。

我使用以下查询选择了所有必需的值:

select `roleid`, 
(select count(`killerid`) from `kills` where `killerid`=`roleid`) as `kills`, 
(select count(`corpseid`) from `kills` where `corpseid`=`roleid`) as `deaths` 
from `characters`

因此,此查询返回角色的ID及其各自的杀戮和死亡人数。

现在,我需要知道:如何更新表“字符”,将行“杀死”和“死亡”设置为查询中返回的值?

感谢您的时间。

1 个答案:

答案 0 :(得分:1)

UPDATE characters c 
  LEFT JOIN 
    ( SELECT killerid, count(*) as kill_num FROM kills GROUP BY killerid ) k1
  ON c.roleid = k1.killerID 
  LEFT JOIN
    ( SELECT corpseid, count(*) as death_num FROM kills GROUP BY corpseid ) k2
  ON c.roleid = k2.corpseID
SET c.kills = k1.kill_num, c.deaths = k2.death_num