每当我在mysql中使用AVG()函数时,它总会返回一个值或null。 这意味着,即使没有结果,它仍然会将行返回为null。
这是我正在使用的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
正如您所看到的,我选择teams
id等于20,此管理器不存在,并且仍然将行返回为null。
我可以看到很多这个问题,但我似乎无法找到帮助我的线索。
答案 0 :(得分:1)
尝试这种方式:
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
GROUP BY
语句与聚合函数(max,min,avg等)一起使用,以将结果集分组为一列或多列。