0

我正在尝试从 2 个表中选择记录,并由unique_idMySQL 中的一列连接。

profile包含每个玩家 1 个唯一条目。

player_current_life包含每个玩家的许多条目,所有条目都player_current_life.is_dead = 1可能包含 1 个条目player_current_life.is_dead = 0

如果玩家在创建新角色之前被杀并离开游戏,他们将不会拥有player_current_life.is_dead = 0. 他们的所有记录都将是player_current_life.is_dead = 1.

我想:在 profile.total_player_kills + player_current_life.player_kills哪里player_current_life.is_dead = '0'

如果player_current_life.is_dead = 0该用户在循环中不存在,则仅 pull profile.total_player_kills

这将循环表中的所有玩家并显示他们过去生命中的总击杀数 (profile.total_player_kills),但如果他们有一个未死的角色 (player_current_life.is_dead = 0),则同时拉取他们当前的生命击杀数。

到目前为止,我所拥有的只有在他们有一个在游戏中还活着的角色时才有效。

SELECT sum(profile.total_player_kills + player_current_life.player_kills) AS All_Player_Kills, profile.name AS Player_Name
FROM
  profile
LEFT OUTER JOIN player_current_life
ON profile.unique_id = player_current_life.unique_id AND player_current_life.is_dead = '0'
WHERE
  profile.total_play_time + player_current_life.play_time >= 10
GROUP BY
  profile.name

因此,简而言之,如果他们有一个还活着的角色,则从profileAND中提取总击杀player_current_life数,如果他们没有,则仅从 中提取击杀数profile

这是我制作的可视化表格,以更好地解释我要完成的工作。

profile (only increases when the player dies)
+-------------+-----------+--------------+
| Player_Name | unique_id | player_kills |
+-------------+-----------+--------------+
| Player1     | 12345     | 25           |
+-------------+-----------+--------------+ 
| Player2     | 67890     + 12           |
+-------------+-----------+--------------+
| Player3     | 54321     + 9            |
+-------------+-----------+--------------+

player_current_life (contains kill count not sent to "profile" where is_dead = 0)
+-------------+--------------+-----------+
| unique_id   | player_kills | is_dead   |
+-------------+--------------+-----------+
| 12345       | 8            | 0         |
+-------------+--------------+-----------+
| 12345       | 15           | 1         |
+-------------+--------------+-----------+
| 12345       | 10           | 1         |
+-------------+--------------+-----------+
| 67890       + 7            | 1         |
+-------------+--------------+-----------+
| 67890       + 5            | 1         |
+-------------+--------------+-----------+
| 54321       + 2            | 1         |
+-------------+--------------+-----------+
| 54321       + 7            | 1         |
+-------------+--------------+-----------+
| 54321       + 5            | 0         |
+-------------+--------------+-----------+

输出应该是:

玩家 1 击杀数 = 33 (25 + 8)

Player2 击杀数 = 12 (12 + 0)

Player3 击杀数 = 14 (9 + 5)

谢谢!

4

2 回答 2

0

您可以在 SELECT 子句中使用if() 语句

SELECT sum(profile.total_player_kills + IF(player_current_life.is_dead = '0', player_current_life.player_kills, 0)) AS All_Player_Kills, profile.name AS Player_Name
FROM
  profile
LEFT OUTER JOIN player_current_life
ON profile.unique_id = player_current_life.unique_id 
WHERE
  profile.total_play_time + player_current_life.play_time >= 10
GROUP BY
  profile.name
于 2013-03-14T15:12:25.427 回答
0

这不是很好的做法,但如果is_dead活着的玩家为零,死去的玩家总是 1,那么(1-is_dead)温暖的玩家将是 1,死者的玩家将是 0。

然后你可以做类似的事情

SELECT (profile.total_player_kills
    + SUM((1-is_dead) * player_kills)) AS All_Player_Kills,
    profile.name AS Player_Name
FROM
  profile
LEFT OUTER JOIN player_current_life
    ON profile.unique_id = player_current_life.unique_id
WHERE
    profile.total_play_time + player_current_life.play_time >= 10
GROUP BY
    profile.name

死人的player_kills乘以0,因此被有效地忽略了。

更好的方法是

SELECT (profile.total_player_kills
    + SUM( CASE WHEN is_dead = 0 THEN player_kills ELSE 0 END) AS ...

那更健壮,可以更容易地适应is_dead布尔类型。

更好的是我会选择一个子选择:

SELECT profile.total_player_kills + this_life.total_player_kills AS ...

FROM profile
LEFT OUTER JOIN

    ( SELECT unique_id, SUM(CASE WHEN is_dead = '0' THEN player_kills ELSE 0 END)
      AS total_player_kills
    FROM player_current_life
    GROUP BY unique_id ) AS this_life

ON (profile.unique_id = this_life.unique_id)
WHERE ...

上面的版本有一个你可以查看的SQLFiddle 。

注意:我相信您的这段代码可能无法正常工作:

SELECT sum(profile.total_player_kills + ...

因为对于当前生命表中的每个元组,总玩家击杀数都会添加一次,而不是每个玩家一次。

于 2013-03-14T15:13:22.830 回答