MySQL query with time and random selection

I would like to know how to make a request 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 would like to get the following:

  • Select all servers where lastPollless than X seconds ago
  • Then select a random entry from the return value

Is it possible? How do I achieve this?

+4
source share
1 answer

You can use something like this:

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

Source: https://habr.com/ru/post/1648942/


All Articles