MySQL 为每个 id 选择最低时间日期



我有这个表:

CREATE TABLE IF NOT EXISTS `event_showtime` (
  `id_show` int(11) NOT NULL AUTO_INCREMENT,
  `id_event` int(11) NOT NULL,
  `latitude` double NOT NULL,
  `longitude` double NOT NULL,
  `event_date_time` datetime NOT NULL,
  PRIMARY KEY (`id_show`),
  KEY `id_event` (`id_event`)
)

值如下所示,例如:

INSERT INTO `event_showtime` (`id_show`, `id_event`, `latitude`, `longitude`, `event_date_time`) VALUES
(1, 1, 49.2016762922894, 18.7615620750428, '2014-03-31 16:13:17'),
(2, 1, 49.2016762922894, 18.7615620750428, '2014-04-01 20:00:00'),
(3, 2, 49.2113914818564, 18.7520992416382, '2014-03-31 15:00:00'),
(4, 2, 49.0545135142313, 20.2952223676682, '2014-04-16 11:00:00'),
(5, 2, 49.2113914818564, 18.7520992416382, '2014-04-23 11:00:00'),
(6, 2, 49.0545135142313, 20.2952223676682, '2014-04-30 11:00:00'),
(7, 2, 49.2016762922894, 18.7615620750428, '2014-04-29 12:00:00'),
(8, 1, 49.2016762922894, 18.7615620750428, '2014-04-24 12:00:00');

我想以这样的方式选择结果:

对于每个id_event,我想选择最近的日期,这也是> NOW(),所以结果应该看起来像:

`id_event` `event_date_time` 
1 , '2014-04-24 12:00:00' 
2 , '2014-04-16 11:00:00'

如何实现这一点? :)

select id_event, min(event_date_time) as lowest_date
from event_showtime
where event_date_time > now()
group by id_event

相关内容

  • 没有找到相关文章

最新更新