MySQL中的算法,用于计算一分钟内的操作数

时间:2015-04-12 00:04:46

标签: mysql datetime

我在MySQL中有这个表:

 id  |           Action             |         DateTime           |   Time(ms)  
-----+------------------------------+----------------------------+-------------
  1  | Player1_rightKeyPress_Start  | 2014-10-01 18:51:17.795000 |   7322
  2  | Player1_shootKeyPress_Start  | 2014-10-01 18:51:17.971000 |   7498
  3  | Player1_rightKeyPress_Start  | 2014-10-01 18:51:18.035000 |   7562
  4  | Player2_leftKeyPress_Start   | 2014-10-01 18:51:18.126000 |   7653
  5  | Player1_shootKeyPress_Start  | 2014-10-01 18:51:18.214000 |   7741
  6  | Player1_rightKeyPress_Start  | 2014-10-01 18:51:18.305000 |   7832
  7  | Player2_leftKeyPress_Start   | 2014-10-01 18:51:18.369000 |   7896
  8  | Player1_shootKeyPress_Start  | 2014-10-01 18:51:18.374000 |   7901
  9  | Player1_rightKeyPress_Start  | 2014-10-01 18:51:18.467000 |   7994
 10  | Player1_shootKeyPress_Start  | 2014-10-01 18:51:18.580000 |   8107
 11  | Player1_rightKeyPress_Start  | 2014-10-01 18:51:18.676000 |   8203
 12  | Player1_shootKeyPress_Start  | 2014-10-01 18:51:18.812000 |   8339
 13  | Player1_rightKeyPress_Start  | 2014-10-01 18:51:18.899000 |   8426
 14  | Player1_shootKeyPress_Start  | 2014-10-01 18:51:19.050000 |   8577
 15  | Player1_rightKeyPress_Start  | 2014-10-01 18:51:19.123000 |   8650

这只是数据库中表的一部分。我想要做的是计算每10,000秒内Player1_rightKeyPress_StartPlayer1_shootKeyPress_StartPlayer2_leftKeyPress_Start发生的次数。时间长达600,000毫秒。我没有费心包括所有这些,因为它非常大。

2 个答案:

答案 0 :(得分:1)

您可能会沿着这些方向做一些事情:转换为秒,除以10得到10秒间隔,然后应用该组:

SELECT Action, FROM_UNIXTIME(FLOOR(UNIX_TIMESTAMP(DateTime)/10)), COUNT(*)
   FROM myTable
   GROUP BY Action, FLOOR(UNIX_TIMESTAMP(DateTime)/10) 

检查mysql文档中的时间函数。

答案 1 :(得分:0)

我不确定我是否理解你的问题,但是下面的sql应该计算整个表中Player1_rightKeyPress_Start的出现次数(swap [TABLENAME]):

SELECT COUNT(*) FROM [TABLENAME] WHERE Action = 'Player1_rightKeyPress_Start';

粘贴desc [TABLENAME]SELECT * FROM [TABLENAME] LIMIT 10;的结果会有所帮助。并添加您想要的结果,因为@Sean说也应该有所帮助。