简体   繁体   中英

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. I'm trying to determine just the players who participated in at least 10 games. So I add the following in my request COUNT(DISTINCT(gameid)) > 9 and then received this SQL error.

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

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

See here

The HAVING clause was added to SQL because the WHERE keyword could not be used with aggregate functions.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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