mysql 查询平均,MySQL查询到平均时间

I play a lot of board games and I maintain a site/database which keeps track of several statistics. One of the tables keeps track of various times. It's structure looks like this:

gameName (text - the name of the board game)

numPeople (int - the number of people that played)

timeArrived (timestamp - the time we arrived at the house we are playing the game)

beginSetup (timestamp - the time when we begin to set up the game)

startPlay (timestamp - the time we actually start playing the game)

gameEnd (timestamp - the time the game is finished)

Basically, what I'm wanting to do is use these times to get some interesting/useful info from (like what game on average takes the longest to set up, what game on average takes the longest to play, what game is the longest from arrival to finish, etc...) Normally, I rely way too much on PHP and I would just do a select * ... and grab all the times then do some PHP calculations to find all the stats but I know that MySQL can do all this for me with a query. Unfortunately, I get pretty lost when it comes to more complex queries so I'd like some help.

I'd like some examples of a couple queries and hopefully I can figure out other average time queries once someone gets me started. What would the query look like for longest time on average to play a board game? What about quickest game/time to set up on average?

Additional Info:

drew010 - You have me off to a great start but I'm not getting the results I'd expected. I've give you some real exmples...

I've got a game called Harper and it's been played twice (so there are two records in the database with time entires). Here are what the times look like for it:

beginSetup(1) = 2012-07-25 12:06:03

startPlay(1) = 2012-07-25 12:47:14

gameEnd(1) = 2012-07-25 13:29:45

beginSetup(2) = 2012-08-01 12:06:30

startPlay(2) = 2012-08-01 12:55:00

gameEnd(2) = 2012-08-01 13:40:32

When I then run the query you provided me (and I convert the seconds into hours/minutes/seconds) I get these results (sorry, I don't know how to do the cool table you did):

gameName = Harper

Total Time = 03:34:32

...and other incorrect numbers.

From the numbers, the Average Total Time should be about 1 hour and 24 minutes - not 3 hours and 34 minutes. Any idea why I'd be getting incorrect numbers?

解决方案

Here is a query to get the average setup time and play time for each game, hope it helps:

SELECT

gameName,

AVG(UNIX_TIMESTAMP(startPlay) - UNIX_TIMESTAMP(beginSetup)) AS setupTime,

AVG(UNIX_TIMESTAMP(gameEnd) - UNIX_TIMESTAMP(startPlay)) AS gameTime,

AVG(UNIX_TIMESTAMP(gameEnd) - UNIX_TIMESTAMP(beginSetup)) AS totalTime,

FROM `table`

GROUP BY gameName

ORDER BY totalTime DESC;

Should yield results similar to:

+----------+-----------+-----------+-----------+

| gameName | setupTime | gameTime | totalTime |

+----------+-----------+-----------+-----------+

| chess | 1100.0000 | 1250.0000 | 2350.0000 |

| checkers | 466.6667 | 100.5000 | 933.3333 |

+----------+-----------+-----------+-----------+

I just inserted about 8 test rows with some random data so my numbers don't make sense, but that is the result you would get.

Note that this will scan your entire table so it could take a while depending on how many records you have in this table. It's definitely something you want to run in the background periodically if you have a considerable amount of game records.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值