Problem:
Develop a MySQL query that extracts the three most imminent events from a given database, considering their scheduled start dates.
Table Structure:
Column | Data Type |
---|---|
EVENT_ID | Integer |
EVENT_NAME | String |
EVENT_START_DATE | DATETIME |
Sample Data:
EVENT_ID | EVENT_NAME | EVENT_START_DATE |
---|---|---|
1 | test | 2011-06-01 23:00:00 |
2 | test2 | 2011-06-03 23:00:00 |
3 | test3 | 2011-07-01 23:00:00 |
4 | test4 | 2011-08-09 23:00:00 |
5 | test5 | 2011-06-02 23:00:00 |
6 | test6 | 2011-04-20 23:00:00 |
Query:
The following query retrieves the three events with the closest future start dates:
SELECT event_id FROM Table WHERE EVENT_START_DATE > NOW() ORDER BY EVENT_START_DATE LIMIT 3;
Explanation:
Note:
If events in the past should also be included, remove the WHERE EVENT_START_DATE > NOW() clause.
The above is the detailed content of How to Retrieve the Three Nearest Future Events in MySQL?. For more information, please follow other related articles on the PHP Chinese website!