简体   繁体   English

关于SQL错误的困惑

[英]Confusion about SQL error

I'm a little bit confused with my sql-statement in PHP, because i do not use the GROUP BY function. 我对PHP中的sql语句有些困惑,因为我不使用GROUP BY函数。 I'm trying to determine just the players who participated in at least 10 games. 我试图确定仅参加了至少10场比赛的球员。 So I add the following in my request COUNT(DISTINCT(gameid)) > 9 and then received this SQL error. 因此,我在请求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

i got following error message: 我收到以下错误消息:

#1111 - Invalid use of group function #1111-无效使用组功能

Please tell me if you need more information. 如果您需要更多信息,请告诉我。

You need to use HAVING for your WHERE COUNT(DISTINCT(gameid)) > 9 because you can't use WHERE with aggregates 您需要对WHERE COUNT(DISTINCT(gameid)) > 9使用HAVING ,因为您不能对集合使用WHERE

See here 看这里

The HAVING clause was added to SQL because the WHERE keyword could not be used with aggregate functions. HAVING子句已添加到SQL,因为WHERE关键字不能与聚合函数一起使用。

Something like (not tested): 类似于(未经测试)的内容:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM