MySQL 如何将sql中的AVG函数的小数位数限制为2?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5316121/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 19:07:04 来源:igfitidea点击:
How can I make the decimal places of AVG function in sql limit to 2 only?
提问by PiDO
I want to limit the decimal of the average to 2..
我想将平均值的小数限制为 2..
SELECT grade.GStudNo, AVG(grade.Grade) AS Average, students.LName, students.FName, students.MName, students.Course
FROM students INNER JOIN grade ON students.StudNo = grade.GStudNo
WHERE GSem = '$sem' AND GYear = '$year'
GROUP BY grade.GStudNo
ORDER BY Average ASC LIMIT 3
回答by DhruvPathak
SELECT grade.GStudNo, ROUND( AVG(grade.Grade),2 ) AS Average, students.LName, students.FName, students.MName, students.Course
FROM students INNER JOIN grade ON students.StudNo = grade.GStudNo
WHERE GSem = '$sem' AND GYear = '$year'
GROUP BY grade.GStudNo
ORDER BY Average ASC LIMIT 3
Would round it to two places.
将它四舍五入到两个地方。
回答by Randy
use the ROUND
function to wrap the AVG calculation...
使用该ROUND
函数来包装 AVG 计算...
回答by álvaro Agüero
Maybe you are looking for this :
也许你正在寻找这个:
SELECT grade.GStudNo, CAST(AVG(grade.Grade) AS DECIMAL(10,2)) AS Average, students.LName, students.FName, students.MName, students.Course
FROM students INNER JOIN grade ON students.StudNo = grade.GStudNo
WHERE GSem = '$sem' AND GYear = '$year'
GROUP BY grade.GStudNo
ORDER BY Average ASC LIMIT 3
this get only 2 decimals
这只能得到 2 位小数
CAST(AVG(grade.Grade) AS DECIMAL(10,2))