2016-12-15 71 views
-1

舉例來說,我有數過會計算音樂家將參加多少場音樂會。然後,我想總結他們演奏的音樂會的價格,然後再根據他們演奏的音樂會的數量來計算音樂會的價格。有沒有辦法做到這一點,並將這些值放入視圖中?MySql - 如何基於計數創建平均值?

謝謝!

+2

這個問題太廣泛了。但是,您可以使用MySQL的集合函數,如COUNT()和AVG()以及SUM()。 –

回答

0

有兩種方法可以做到這一點: 1)在MySQL中使用「Average」functino有人建議作爲OP的註釋。 教程可以在這裏找到:http://www.mysqltutorial.org/mysql-avg/

2)您可以通過價格的總和那麼對於音樂會的計數用戶變量,然後,做你的數學第三用戶變量創建用戶變量手工做數學題。然後輸出該數字。這裏是你可能需要的代碼:

# Start by getting our to independent variables we will need to create our average 
SET @prices := sum((#query to find all prices of concerts 
      )); 
SET @concerts := count((#query to count all of the concerts someone belongs to 
       )); 

# do the math to average the numbers 
SET @average := @prices/@concert; 

# Now output that average to the user 
SELECT @average;