MySQL AVG()函数返回错误值

时间:2019-04-16 16:35:21

标签: mysql sql database database-design mysql-workbench

我正在尝试计算所选列的平均值。
如果我运行此代码:

SELECT answer_value
FROM survey$question$answer JOIN answer
ON survey$question$answer.answer_letter = answer.answer_letter
WHERE survey$question$answer.survey_id = 12;

我明白了(这些值是正确的):
enter image description here

但是,当我运行此命令时:

SELECT AVG(answer_value)
FROM survey$question$answer JOIN answer
ON survey$question$answer.answer_letter = answer.answer_letter
WHERE survey$question$answer.survey_id = 12;

我明白了:
enter image description here

我得到2的平均值,而正确的5、5和3平均值是4.33
为什么会这样?

另一个例子。这段代码在这里:

SELECT answer_value
FROM survey$question$answer JOIN answer
ON survey$question$answer.answer_letter = answer.answer_letter
WHERE survey$question$answer.survey_id = 6;

给我看这个:
enter image description here

但是当我尝试使用此代码计算平均值时:

SELECT AVG(answer_value)
FROM survey$question$answer JOIN answer
ON survey$question$answer.answer_letter = answer.answer_letter
WHERE survey$question$answer.survey_id = 6;

我明白了:
enter image description here

3、2和1的平均值等于2,而不是9.33

帮助?
谢谢!

2 个答案:

答案 0 :(得分:0)

我认为您错了,您在以下其他地方确实犯了错误,请检查表列的数据类型

select avg( c) from (select 5 as c
union all
select 5
union all
select 3
) a

 c
4.3333

online demo

答案 1 :(得分:0)

我首先有answer_value作为一组数字。
后来我将answer_value更改为INT,但是忘记删除并重新创建表。 >。<< br /> 现在我做的很好。

谢谢您的时间,对不起我的迟钝...