MySQL - 需要帮助计算与其他表中的某些对应的id

时间:2009-07-20 14:59:13

标签: php optimization mysql query-optimization

我在名为players的表上,然后是名为triesconversionspenaltiesdropgoals的其他表。

我需要从player_id获取players,然后计算以下内容:

  • player_id
  • tries的尝试次数
  • player_id
  • conversions的转化次数
  • player_id
  • penalties的处罚数量
  • player_id
  • dropgoals的dropgoals数量

这一切都需要一次性发生,因为每场比赛大约有15名球员,该网站与橄榄球有关。

我尝试过以下工作:

SELECT players.player_id, players.number, CASE WHEN (COUNT(tries.player_id) = 0) THEN ' ' ELSE COUNT(tries.player_id) END AS nrTries FROM players LEFT JOIN tries ON players.player_id = tries.player_id WHERE players.team_id IS NULL GROUP BY players.player_id ORDER BY players.number

它从players表中选择所有玩家并计算他们的尝试次数,但只要我将其更改为以下内容就会给我一个错误:

SELECT players.player_id, players.number, player.name, CASE WHEN (COUNT(tries.player_id) = 0) THEN ' ' ELSE COUNT(tries.player_id) END AS nrTries FROM players, player LEFT JOIN tries ON players.player_id = tries.player_id WHERE players.player_id = player.player_id AND players.team_id IS NULL GROUP BY players.player_id ORDER BY players.number

出现以下错误:

Unknown column 'players.player_id' in 'on clause'

有人可以帮我解决这个问题吗,我已经苦苦挣扎了好几天了?

提前致谢

//编辑:

大家好,我觉得现在非常愚蠢,这段代码非常出色,除了当玩家有多种类型的得分,比如尝试和转换,或尝试和惩罚时,它会错误计算每种类型的数量,并且它们都是一样的。

让我们说我的球员得到1分和3分,这输出3分和3分,我无法弄清楚是什么问题。

这是我的问题:

SELECT players.player_id, players.number, player.name, player.surname,
CASE WHEN (COUNT(tries.player_id) = 0) THEN '& nbsp;' ELSE COUNT(tries.player_id) END AS nrTries,
CASE WHEN (COUNT(conversions.player_id) = 0) THEN '& nbsp;' ELSE COUNT(conversions.player_id) END AS nrConversions,
CASE WHEN (COUNT(dropgoals.player_id) = 0) THEN '& nbsp;' ELSE COUNT(dropgoals.player_id) END AS nrDropgoals,
CASE WHEN (COUNT(penalties.player_id) = 0) THEN '& nbsp;' ELSE COUNT(penalties.player_id) END AS nrPenalties
FROM players
LEFT JOIN tries ON players.player_id = tries.player_id AND tries.game_id = '$game_id'
LEFT JOIN conversions ON players.player_id = conversions.player_id AND conversions.game_id = '$game_id'
LEFT JOIN dropgoals ON players.player_id = dropgoals.player_id AND dropgoals.game_id = '$game_id'
LEFT JOIN penalties ON players.player_id = penalties.player_id AND penalties.game_id = '$game_id'
LEFT JOIN player ON players.player_id = player.player_id
WHERE players.player_id = player.player_id AND players.team_id IS NULL AND players.game_id = '$game_id'
GROUP BY players.player_id ORDER BY players.number

请注意: $ game_id是一个PHP变量。
另外: 我在&之间加了一个空格并且否则它不会被输入SO。

有人可以指出我正确的方向吗?

3 个答案:

答案 0 :(得分:3)

你对这些数据还不够具体,但假设每个玩家的每个关联表中都有一条记录,如果不存在则你很乐意显示NULL:

SELECT player_id, tries, conversions, penalties, dropgoals
FROM players p
LEFT JOIN tries t ON t.player_id = p.player_id
LEFT JOIN conversions c ON c.player_id = p.player_id
LEFT JOIN penalties e ON e.player_id = p.player_id
LEFT JOIN dropgoals d ON d.player_id = p.player_id

这可以重申为:

SELECT player_id
(SELECT tries FROM tries WHERE player_id = p.player_id) tries,
(SELECT conversions FROM conversions WHERE player_id = p.player_id) conversions,
(SELECT penalties FROM penalties WHERE player_id = p.player_id) penalties,
(SELECT dropgoals FROM dropgoals WHERE player_id = p.player_id) dropgoals
FROM players p

根据您的数据库引擎,性能可能会有所不同。如果您需要对此求和,请将其更改为:

SELECT player_id
(SELECT SUM(tries) FROM tries WHERE player_id = p.player_id) tries,
(SELECT SUM(conversions) FROM conversions WHERE player_id = p.player_id) conversions,
(SELECT SUM(penalties) FROM penalties WHERE player_id = p.player_id) penalties,
(SELECT SUM(dropgoals) FROM dropgoals WHERE player_id = p.player_id) dropgoals
FROM players p

如果需要,上述任何一种都可以使用IFNULL()或类似函数返回0而不是NULL。

答案 1 :(得分:1)

我有点困惑为什么你说Cletus有正确的解决方案,并在更新原始问题时继续使用它。也就是说,Cletus的解决方案略有偏差,你需要COUNT()而不是SUM()。请尝试以下方法:

SELECT players.player_id,
(SELECT COUNT(*) FROM tries WHERE player_id = players.player_id) tries,
(SELECT COUNT(*) FROM penalties WHERE player_id = players.player_id) penalties
FROM players;

这将返回0而不是& nbsp我建议在您的应用程序代码中处理它。如果你真的希望从mysql中获取& nbsp,你可以加入CASE混乱。

答案 2 :(得分:0)

为什么第二个查询引用“播放器”?我没有看到你在其他任何地方提到这样的桌子......(“来自玩家,玩家”)只是确保这是故意的。

避免将显式连接(例如“LEFT JOIN”)与隐式连接(例如“FROM players,player”)混合使用可能会有所帮助,以保持结构的一致性。