简体   繁体   English

SQL函数avg()始终返回true(空)

[英]SQL function avg() always returns true (null)

Whenever i use the AVG() function in mysql, it will always return a value or null. 每当我在mysql中使用AVG()函数时,它将始终返回一个值或null。 Which means, even is there is no result, it will still returns the rows as null. 这意味着,即使没有结果,它仍将返回空行。

Here is the sql command I'm using: 这是我正在使用的sql命令:


SELECT teams.id, AVG(players.skill) AS "rating", teams.name, credit, date, leagues.name AS "league_name"
FROM (teams)
LEFT JOIN leagues ON leagues.id = teams.league
LEFT JOIN players ON teams.id = players.team
WHERE manager = '20'
LIMIT 1

As you can see, i select where manager id equal 20, this manager doesn't exist, and it still returns rows as null. 如您所见,我选择manager ID等于20的地方,该经理不存在,并且它仍将行返回为null。

I can see alot of that this issue, however I cant seem to find a thread that helps me. 我可以看到很多关于此问题的信息,但是我似乎找不到能够帮助我的线程。

Try this way: 尝试这种方式:

SELECT teams.id,  
       teams.name, 
       credit, 
       date, 
       leagues.name AS "league_name",
       AVG(players.skill) AS "rating"
  FROM teams
     LEFT JOIN leagues ON leagues.id = teams.league
     LEFT JOIN players ON teams.id = players.team
 WHERE teams.manager = '20'
 GROUP BY teams.id,  
       teams.name, 
       credit, 
       date, 
       leagues.name
 LIMIT 1

The GROUP BY statement is used in conjunction with the aggregate functions (max, min, avg, etc) to group the result-set by one or more columns. GROUP BY语句与聚合函数(最大值,最小值,平均值等)结合使用,以按一列或多列对结果集进行分组。

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

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