2016-12-24 446 views
0

我有以下SQL查詢:如何在此sql查詢中將小數位數限制爲2?

select u.user_name, sum(r.goals) total_goals, sum(r.goals)/(2 * count(1)) avg_goals 
from (select community_id, player1_id id, player1_goals goals 
from results union all select community_id, player2_id, player2_goals from results) r 
inner join users u on r.id = u.id 
where r.community_id = 16 group by r.id, u.user_name 
ORDER BY avg_goals DESC 

其產生的結果,如 "avg_goals" , "1.2500"(我只在此列此實例興趣)

如何限制小數位的結果只有2?所以輸出將1.25而不是1.2500

回答

3

可以使用ROUND(col, 2)

select u.user_name, sum(r.goals) total_goals, round(sum(r.goals)/(2 * count(1)),2) avg_goals 
from (select community_id, player1_id id, player1_goals goals 
from results union all select community_id, player2_id, player2_goals from results) r 
inner join users u on r.id = u.id 
where r.community_id = 16 group by r.id, u.user_name 
ORDER BY avg_goals DESC 

正如@scaisEdge說,您也可以使用截斷(COL,2)如果你只是要截斷的數字,其餘。

0

你應該使用TRUNCATE例如:

SELECT TRUNCATE(1.2500,2) from dual ; 


    SELECT TRUNCATE(round(sum(r.goals)/(2 * count(1)),2), 2) ..... 
相關問題