I want to be able to retrieve rows that were inserted into a mysql table within the last 4 minutes. It may help that I have got a mysql datetime field called 'when' and I insert into it the mysql time at the point of insertion with the now 'NOW()' e.g.
$query = "INSERT INTO user_actions SET user='" . $user_id . "'" .
", action='" . $server_action . "'" .
", `when`=NOW()";
Is there an elegant way to SELECT * FROM user_actions WHERE ... (age of the row is younger than 4 minutes and action = 'some value') Feel free to disregard the 'when' if it is not needed.
Regards, Stuart
SELECT * FROM user_actions
WHERE `when` >= addtime(now(), '-00:04:00')
or
SELECT * FROM user_actions
WHERE `when` >= now() - interval 4 minute
You can use DATE_ADD, see here for documentation.
Example:
SELECT * FROM `user_actions` WHERE `when` >= DATE_ADD( NOW(), INTERVAL -4 MINUTE )
SELECT * FROM user_actions
WHERE `when` >= DATE_ADD(now(), INTERVAL -4 MINUTE)
and `action` ='someAction'