How can I make the decimal places of AVG function

2020-08-09 08:25发布

I want to limit the decimal of the average to 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

标签: mysql
3条回答
乱世女痞
2楼-- · 2020-08-09 08:33
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.

查看更多
老娘就宠你
3楼-- · 2020-08-09 08:45

use the ROUND function to wrap the AVG calculation...

查看更多
狗以群分
4楼-- · 2020-08-09 08:51

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

CAST(AVG(grade.Grade) AS DECIMAL(10,2))
查看更多
登录 后发表回答