Tijl Van den Brugghen Tijl Van den Brugghen - 4 months ago 4
PHP Question

MySQL query with time and random selecting

I'd like to know how to how to make a query that does this:

I have a table like this:

CREATE TABLE `sendingServers` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` text NOT NULL,
`address` text NOT NULL,
`token` text NOT NULL,
`lastPoll` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;


And I'd like to get the following:


  • Select all servers where
    lastPoll
    is less then X seconds ago

  • Then select a random entry from the return value



Is this possible ? How do I achieve that ?

Answer

You can use something like this:

select * from `sendingServers`
where  `lastPoll` > DATE_SUB(NOW(), INTERVAL 30 SECOND) 
order by rand() limit 1
Comments