MySql - 如何根据计数创建平均值?

时间:2016-12-15 14:12:16

标签: mysql

例如,我可以计算一下音乐家将演奏多少场音乐会。我想总结他们演奏的音乐会的价格,然后是/他们演奏的音乐会的数量。是否有这样做的方法并将这些值放入视图中?

谢谢!

1 个答案:

答案 0 :(得分:0)

有两种方法可以做到这一点: 1)使用MySQL中的“平均”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;
相关问题