Home > Database > Mysql Tutorial > How to Add Days to Dates in MySQL Using DATE_ADD()?

How to Add Days to Dates in MySQL Using DATE_ADD()?

Barbara Streisand
Release: 2025-01-24 06:37:09
Original
918 people have browsed it

How to Add Days to Dates in MySQL Using DATE_ADD()?

MySQL date addition and subtraction: use DATE_ADD() function

MySQL provides the DATE_ADD() function to easily modify date values, which can add or subtract specific time intervals to a specified date.

Add the number of days to the current date value

To add the number of days to the current date value in a MySQL table, use the following syntax:

<code class="language-sql">SELECT DATE_ADD(你的字段名, INTERVAL 2 DAY)
FROM 表名;</code>
Copy after login

Replace 你的字段名 with the name of the date field in the table. INTERVAL 2 DAY means to add two days to the current date.

Example:

Suppose there is a table named classes:

<code class="language-sql">CREATE TABLE classes (
  id INT NOT NULL AUTO_INCREMENT,
  date DATE NOT NULL,
  PRIMARY KEY (id)
);</code>
Copy after login

To add two days to the date value for row id 161, use the following query:

<code class="language-sql">UPDATE classes 
SET date = DATE_ADD(date, INTERVAL 2 DAY)
WHERE id = 161;</code>
Copy after login

This query will increase the date value in the specified row by two days.

Note: If the date field is of type datetime, the same syntax can be used, but the time interval should be specified in hours, minutes, and seconds instead of days. For example, INTERVAL 2 HOUR adds two hours to the current date and time.

The above is the detailed content of How to Add Days to Dates in MySQL Using DATE_ADD()?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template