关于SQL错误的困惑

时间:2016-02-11 21:20:20

标签: php mysql

我对PHP中的sql语句有点困惑,因为我没有使用GROUP BY函数。我试图确定参加至少10场比赛的球员。所以我在请求COUNT(DISTINCT(gameid)) > 9中添加以下内容,然后收到此SQL错误。

SELECT 
  SUM(kills) as sum_kills, 
  SUM(deaths) as sum_death, 
  SUM(teamkills) as sum_teamkills, 
  SUM(suizide) as sum_suizide, 
  SUM(points) as sum_points, 
  aliases.hash as name, 
  aliases.rang as rang, 
  COUNT(DISTINCT(gameid)) as sum_games, 
  playerid 
FROM 
  stats_rounds_players, 
  aliases 
WHERE 
  aliases.id = playerid 
  AND COUNT(DISTINCT(gameid)) > 9 
  AND aliases.hash != ''
GROUP BY 
  playerid 
ORDER BY 
  sum_points DESC

我收到以下错误消息:

  

#1111 - 无效使用群组功能

如果您需要更多信息,请告诉我。

1 个答案:

答案 0 :(得分:1)

您需要对HAVING使用WHERE COUNT(DISTINCT(gameid)) > 9,因为您无法将WHERE与聚合一起使用

See here

  

HAVING子句已添加到SQL,因为WHERE关键字不能   与聚合函数一起使用。

像(未经测试)的东西:

SELECT 
  SUM(kills) as sum_kills, 
  SUM(deaths) as sum_death, 
  SUM(teamkills) as sum_teamkills, 
  SUM(suizide) as sum_suizide, 
  SUM(points) as sum_points, 
  aliases.hash as name, 
  aliases.rang as rang, 
  COUNT(DISTINCT(gameid)) as sum_games, 
  playerid 
FROM 
  stats_rounds_players, 
  aliases 
WHERE 
  aliases.id = playerid 
  AND aliases.hash != ''
GROUP BY 
  playerid 
HAVING 
  COUNT(DISTINCT(gameid)) > 9 
ORDER BY 
  sum_points DESC