我有点卡住了..
我有两张桌子
mysql > EXPLAIN event;
+-------------+--------------+------+-----+---------------------+-----------------------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------------------+-----------------------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(100) | NO | MUL | | |
| extUrl | varchar(192) | NO | | | |
+-------------+--------------+------+-----+---------------------+-----------------------------+
一个事件可以有多个
mysql> explain eventDate;
+----------------+--------------+------+-----+---------------------+-----------------------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+--------------+------+-----+---------------------+-----------------------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| category_id | int(11) | NO | MUL | 0 | |
| event_id | int(11) | NO | MUL | 0 | |
| location_id | int(11) | NO | MUL | 0 | |
| name | varchar(100) | NO | | | |
| eventDate | timestamp | NO | | 0000-00-00 00:00:00 | |
+----------------+--------------+------+-----+---------------------+-----------------------------+
我想通过 查询事件id
,location_id
下一个即将到来 eventDate
的事件,我被困在如何形成该查询
我目前有这个
SELECT
e.id,
d.location_id,
d.eventDate
FROM
event e
INNER JOIN eventDate d ON d.event_id = e.id AND d.eventDate >= CURRENT_TIMESTAMP
WHERE
e.id = 5107
GROUP BY
e.id
ORDER BY
d.eventDate ASC
现在这会返回一个结果,但是我 GROUP BY 事件,所以我不确定如何确保真的选择了下一个 eventDate。此外,我没有使用任何聚合函数( group by one, aggregate many ),这似乎不是一个干净且明确定义的结果要求 - MySQL 现在随机选择其中一个 eventDate 行不是吗?