我正在使用以下查询获取有关这些表的信息,但是 defenderhit 和 deferdamage SUM 值与第一个连接的行号的行数相乘。
表'战斗':
battle_id city_id attacker defender battle_time
1 07 6 0 1342918014
表'战斗打击':
battle_id family_id user_id hits damage
1 0 0 1000 50000
1 6 15 108 3816
1 6 2 81 2046
1 6 1 852 1344
MySQL 查询:
SELECT b.battle_id, b.city_id, b.attacker, b.defender, b.battle_time,
SUM(COALESCE(bh1.damage,0)) AS attackerdamage, SUM(COALESCE(bh2.damage,0)) AS defenderdamage,
SUM(COALESCE(bh1.hits,0)) AS attackerhit, SUM(COALESCE(bh2.hits,0)) AS defenderhit
FROM battles AS b
LEFT JOIN battlehits AS bh1 ON b.attacker = bh1.family_id
LEFT JOIN battlehits AS bh2 ON b.defender = bh2.family_id
WHERE b.battle_id=1
GROUP BY b.battle_id LIMIT 1
该查询的结果如下:
battle_id city_id attacker defender battle_time attackerdamage defenderdamage attackerhit defenderhit
1 07 6 0 1342918014 7206 150000 1041 3000
正如您在表数据中看到的,defenderhit 和 deferdamage SUM 值应该是 1000 和 50000,但它们乘以 3。我在这里做什么?有什么问题?
提前致谢。