在 MySQL 中获取当前日期

Victor A. Oguntuase 2023年10月8日
  1. 使用 curdate() 函数获取 MySQL 中的当前日期
  2. 使用 now() 函数获取 MySQL 中的当前日期
在 MySQL 中获取当前日期

使用数据库时,通常会出现各种用例来实现。当需要从系统中获取当前日期进行计算时,MySQL 中的 curdate()now() 函数是可行的工具。

使用 curdate() 函数获取 MySQL 中的当前日期

根据此函数的官方文档,它返回查询的当前日期的值。此返回是日期数据类型,可以为数据库中的信息或计算目的进行格式化或操作。

curdate() 函数具有以相同方式工作的同义词函数,即 current_date()current_date

curdate() 函数及其同义词的使用在以下代码块中进行了说明。

-- Testing out various current date functions in mysql
SELECT curdate() as "Today's date with curdate";
SELECT current_date() as "Today's date with current_date()";
SELECT current_date as "Today's date with current_date";

输出:

Today's date with curdate
2022-02-03

0.000 sec / 0.000 sec
1 row(s) returned

-----------------------------------------------------------------------------------------
Today's date with current_date()
2022-02-03

0.015 sec / 0.000 sec
1 row(s) returned

-----------------------------------------------------------------------------------------
Today's date with current_date
2022-02-03

0.000 sec / 0.000 sec
1 row(s) returned

从查询的速度可以看出,curdate() 函数获取当前日期的速度足够快。此外,它的同义词/别名之一 current_date() 需要更长的时间。

虽然对于小型数据集和查询来说,几秒钟似乎微不足道,但对于大规模应用程序来说,就会出现严重的问题。

使用 now() 函数获取 MySQL 中的当前日期

now() 函数,与 curdate() 函数或其同义词/别名相反,返回当天的日期时间。

由于 datetime 由两部分组成,即日期和时间,因此需要一个外部日期抓取函数,如 extract()date() 来获取所需的数据。

可通过此文档获得从日期时间中提取日期元素的良好参考。

-- Illustrating the now() function
SELECT now() as "Today's datetime", date(now()) as "Today's date";

输出:

Today's datetime		Today's date
2022-02-03 20:25:03		2022-02-03

-----------------------------------------------------------------------------------------
0.000 sec / 0.000 sec
1 row(s) returned

now() 函数还有同义词,如 CURRENT_TIMESTAMP()CURRENT_TIMESTAMPLOCALTIME()LOCALTIMELOCALTIMESTAMP()LOCALTIMESTAMP

让我们考虑一个简单的例子,说明为什么可能需要获取当前日期。让我们创建一个名为 students 的示例表,其中包含 idnamedate_of_birth

/* Creating a sample table for illustrating a use-case of the current date methods */
CREATE TABLE students(
	id INT AUTO_INCREMENT,
    name VARCHAR(255),
    date_of_birth date,

    PRIMARY KEY(id)
);

-- Populating the students table
INSERT INTO students(name, date_of_birth) VALUES
	('Susan Doyle', '1991-02-24'),
    ('James Maddison', '1991-07-22'),
    ('Christine Pile', '1993-09-02'),
    ('Damien Black', '1987-03-14');

-- Viewing the table
SELECT * FROM students;

输出:

id	name			date_of_birth
1	Susan Doyle		1991-02-24
2	James Maddison	1991-07-22
3	Christine Pile	1993-09-02
4	Damien Black	1987-03-14
-----------------------------------------------------------------------------------------
0.969 sec
0 row(s) affected

0.516 sec
4 row(s) affected Records: 4  Duplicates: 0  Warnings: 0

0.000 sec / 0.000 sec
4 row(s) returned

现在,让我们添加一个名为 age 的额外列。此列将计算每个学生的年龄作为当年与其出生日期之间的差异。

-- Modifying the existing table to add a new column
ALTER TABLE students
ADD AGE TINYINT;

输出:

0.766 sec
0 row(s) affected Records: 0  Duplicates: 0  Warnings: 0

请注意,该值是使用 TINYINT 数据类型存储的。像这样存储值可以提高内存效率,因为我们的年龄数据很小。

这是官方文档中关于各种整数数据类型。

现在我们可以更新 AGE 列的值。

/* Updating the table with values. The 'generated always' constraint would have been an excellent way to implement this. However, it does not accept the curdate() nor now() function (nor their aliases) */

UPDATE students
SET AGE = (SELECT (YEAR(CURDATE()) - YEAR(students.date_of_birth))) -- using curdate()
WHERE students.id > 0;

输出:

0.140 sec
4 row(s) affected Rows matched: 4  Changed: 4  Warnings: 0

让我们预览一下我们的决赛桌。

SELECT * FROM students;

输出:

id	name			date_of_birth	AGE
1	Susan Doyle		1991-02-24		31
2	James Maddison	1991-07-22		31
3	Christine Pile	1993-09-02		29
4	Damien Black	1987-03-14		35
-----------------------------------------------------------------------------------------
0.000 sec / 0.000 sec
4 row(s) returned

当前的方法有效。但是,可以改进结果以正确更新生日距离当前日期还很远的学生的年龄。

我们将 DATEDIFF 函数与 FLOOR 函数相结合来实现此结果。

-- Improving the age update algorithm
UPDATE students
SET AGE = (SELECT FLOOR(DATEDIFF(CURDATE(),students.date_of_birth)/365))
WHERE students.id > 0;

SELECT * FROM students;

输出:

id	name			date_of_birth	AGE
1	Susan Doyle		1991-02-24		30
2	James Maddison	1991-07-22		30
3	Christine Pile	1993-09-02		28
4	Damien Black	1987-03-14		34
-----------------------------------------------------------------------------------------
0.093 sec
4 row(s) affected Rows matched: 4  Changed: 4  Warnings: 0

0.000 sec / 0.000 sec
4 row(s) returned
Victor A. Oguntuase avatar Victor A. Oguntuase avatar

Victor is an experienced Python Developer, Machine Learning Engineer and Technical Writer with interests across various fields of science and engineering. He is passionate about learning new technologies and skill and working on challenging problems. He enjoys teaching, intellectual discourse, and gaming, among other things.

LinkedIn GitHub

相关文章 - MySQL Date