MySQL为每个id选择最低时间

时间:2014-04-06 11:41:44

标签: mysql sql datetime select

我有这张桌子:

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');

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

对于每个id_event,我想选择最近的日期,也就是>现在()所以结果应该是这样的:

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

如何实现这一目标? :)

1 个答案:

答案 0 :(得分:0)

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