乐闻世界logo
搜索文章和话题

How to get current date & time in MySQL?

1个答案

1

In MySQL, several built-in functions can be used to retrieve the current date and time. The most commonly used function is NOW(), which returns a value representing the current date and time in the format YYYY-MM-DD HH:MM:SS.

For example, if you want to use the current date and time in a query, you can write the following SQL statement:

sql
SELECT NOW();

This statement returns the date and time when the query is executed.

Additionally, there are other functions that can be used to retrieve the current date or time, such as:

  • CURDATE(): Returns the current date in the format YYYY-MM-DD.
  • CURTIME(): Returns the current time in the format HH:MM:SS.

If you only need a specific part of the date or time, you can use functions such as YEAR(), MONTH(), DAY(), HOUR(), MINUTE(), and SECOND() to extract the year, month, day, hour, minute, or second from the result of NOW(). For example:

sql
SELECT YEAR(NOW()), MONTH(NOW()), DAY(NOW());

This statement returns the current year, month, and day.

Using these functions enables you to handle and utilize date and time data flexibly in database operations.

2024年8月7日 09:52 回复

你的答案