2011-03-15 481 views
11

我想一般的小數限制在2 ..如何將sql限制中的AVG函數的小數位數設置爲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 
+0

這個問題是指MySQL嗎?如果是這樣,請妥善標記。 – 2011-03-15 18:15:50

+0

是的,這是一個MySQL問題 – PiDO 2011-03-15 18:17:27

回答

12

使用ROUND功能來包裝AVG計算...

34
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 

難道四捨五入到兩個地方。

相關問題