MySQL query -> using time to alter an int value and sort it
I'm making a multiplayer highscore table to a game. Scores should decrease by 1 point per hour.开发者_运维百科 Is it possible to make a query that calculates how many hours passed since last time a score was submitted by using a timestamp and an integer? I've looked at TIME_TO_SEC() but how do I combine that function with the score-column?
In a common language this is something I'd like to do:
float getScore(int timePassedInSeconds, float score)
{
return score-((float)timePassedInSeconds/3600.0f);
}
And then sort the table based on what that function returns...
make an UNSIGNED INT last_modified column (you can make it a TIMESTAMP but that makes arithmetic operations a little confusing).
CREATE TABLE scores( score FLOAT, last_modified INT UNSIGNED, ... );
and do your update operations like this:
UPDATE scores SET
score = score - ( ( CURRENT_TIMESTAMP() - last_modified ) / 3600.0 ),
last_modified = CURRENT_TIMESTAMP() ...
精彩评论