简体   繁体   English

MySQL查询时间和随机选择

[英]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 选择lastPoll小于X秒前的所有服务器
  • Then select a random entry from the return value 然后从返回值中选择一个随机条目

Is this possible ? 这可能吗 ? How do I achieve that ? 我如何实现这一目标?

You can use something like this: 你可以使用这样的东西:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM