1
我有一个表:Mysql:如何计算两个avg之间的差异?
+------------+--------------+-------+------------+
| Name | Nation | Score | Game_date |
+------------+--------------+-------+------------+
| Ginobili | Argentina | 48 | 2005-01-21 |
| Irving | Australia | 44 | 2014-04-06 |
| Kirilenko | Soviet Union | 31 | 2006-11-11 |
| Kobe | USA | 81 | 2006-01-22 |
| LeBron | USA | 52 | 2014-12-06 |
| Mutombo | Congo | 39 | 1992-02-03 |
| Nowitzki | Germany | 48 | 2011-05-18 |
| PauGasol | Spain | 46 | 2015-01-11 |
| SteveNash | Canada | 42 | 2006-12-08 |
| TonyParker | France | 55 | 2008-11-06 |
| YaoMing | China | 41 | 2009-02-23 |
| YiJianlina | China | 31 | 2010-03-27 |
+------------+--------------+-------+------------+
我要计算的AVG(分数)美国 - 中国的AVG(分数)。
我已经试过
select avg(score) from nba where nation = "USA" -
avg(score) from nba where nation = "China";
但它是错的!
会不是这样的短语需要一个 '否则为0'? –
@WhirlMind在SQL Server中,如果未提供ELSE NULL,则为默认值。在MySQL中不需要看[demo](http://sqlfiddle.com/#!9/9eecb7d/29291/0):)无论如何你不能添加'ELSE 0',因为它会改变结果。 AVG仅适用于非价值 – lad2025
现在我看到UNION选项,我想这可能是一个完美的选择。 另外两件事情,就案例而言: (a)在SQL Server中,假定null可能会导致聚合尝试出现空警告? (b)如果我们确实包括了0,那么平均值是否会将总数除以合格行数或所有行数? –