Worldscope

MySQL Interval

Palavras-chave:

Publicado em: 23/08/2025

Understanding MySQL INTERVAL

The MySQL `INTERVAL` keyword is a powerful tool for adding or subtracting a specific amount of time to a date or datetime value. This article provides a comprehensive guide to using `INTERVAL` in MySQL, covering its syntax, common use cases, and alternative approaches. We'll explore how to use `INTERVAL` effectively to manipulate dates and times within your MySQL queries.

Fundamental Concepts / Prerequisites

Before diving into the details of `INTERVAL`, it's essential to have a basic understanding of MySQL date and datetime data types. You should be familiar with the `DATE`, `DATETIME`, `TIMESTAMP`, and `TIME` data types. Additionally, a basic understanding of SQL queries, including `SELECT` statements and date functions like `NOW()` and `CURDATE()`, is assumed.

Core Implementation/Solution

The `INTERVAL` keyword is used with operators like `+` (addition) and `-` (subtraction) to modify date and datetime values. The general syntax is:


SELECT date_expression + INTERVAL value unit;
SELECT date_expression - INTERVAL value unit;

Where:

  • `date_expression` is a date, datetime, or timestamp value or expression.
  • `value` is a numerical value specifying the amount of time to add or subtract.
  • `unit` is a keyword specifying the unit of time (e.g., `YEAR`, `MONTH`, `DAY`, `HOUR`, `MINUTE`, `SECOND`).

Here's an example demonstrating different uses of `INTERVAL`:


-- Get the date one week from today
SELECT CURDATE() + INTERVAL 1 WEEK AS next_week;

-- Get the datetime 30 minutes from now
SELECT NOW() + INTERVAL 30 MINUTE AS thirty_minutes_later;

-- Get the date 1 year and 2 months from a specific date
SELECT '2023-01-15' + INTERVAL '1-2' YEAR_MONTH AS future_date;

-- Get the date 5 days ago
SELECT CURDATE() - INTERVAL 5 DAY AS five_days_ago;

-- Insert a future date into a table
INSERT INTO events (event_date) VALUES (NOW() + INTERVAL 1 DAY);

Code Explanation

The first example uses `CURDATE()` to get the current date and adds an interval of 1 week using `INTERVAL 1 WEEK`. The result is aliased as `next_week`.

The second example uses `NOW()` to get the current datetime and adds 30 minutes using `INTERVAL 30 MINUTE`. The result is aliased as `thirty_minutes_later`.

The third example shows how to add a combination of years and months. `'1-2'` is interpreted as 1 year and 2 months, which is added to the date '2023-01-15'. The `YEAR_MONTH` unit tells MySQL to interpret the interval as year and month values. It can accept a string format of 'years-months'.

The fourth example demonstrates subtraction. It subtracts 5 days from the current date using `INTERVAL 5 DAY`.

The fifth example shows how you could use the interval in the context of data insertion, ensuring future event records are correctly entered.

Complexity Analysis

The time complexity of using `INTERVAL` is generally considered to be O(1) because the addition or subtraction operation is a constant-time operation. The database engine performs the date/time arithmetic directly without iterating over a large dataset. The space complexity is also O(1) because only a constant amount of memory is used to store the result of the calculation.

Alternative Approaches

While `INTERVAL` is the most common and recommended approach, you can also use date and time functions like `DATE_ADD()` and `DATE_SUB()` to achieve similar results. For example, the following is equivalent to `SELECT CURDATE() + INTERVAL 1 WEEK;`:


SELECT DATE_ADD(CURDATE(), INTERVAL 1 WEEK);

The trade-off is that `DATE_ADD()` and `DATE_SUB()` can be slightly more verbose, especially when dealing with complex intervals. `INTERVAL` often offers a more readable and concise syntax. However, `DATE_ADD` and `DATE_SUB` might be preferable in contexts where you need to ensure compatibility with older MySQL versions or when working with parameterized values for the interval.

Conclusion

The `INTERVAL` keyword is a valuable tool in MySQL for manipulating date and datetime values. It offers a concise and readable way to add or subtract specific amounts of time, making it essential for various tasks, such as calculating future dates, determining time differences, and managing event schedules. By understanding the syntax and available units, you can effectively leverage `INTERVAL` to enhance your SQL queries and database applications.