mysql 出现多个now_MySQL NOW() 函数

Summary: in this tutorial, you will learn about theMySQL NOW()function and apply theNOWfunction in various contexts.

Introduction to MySQL NOW() function

The MySQLNOW()function returns the current date and time in the configured time zone as a string or a number in the'YYYY-MM-DD HH:MM:DD'or'YYYYMMDDHHMMSS.uuuuuu'format.

The returned type of theNOW()function depends on the context where it is used. For example, in the following statement, theNOW()function returns the current date and time as a string:SELECTNOW();

article-114020-1.html

However, in the numeric context as the following example, theNOW()function returns the current date and time as a number:SELECTNOW()+0;

article-114020-1.html

Notice that theNOW()function returns a constant date and time at which the statement started executing. See the following example:SELECTNOW(),SLEEP(5),NOW();

article-114020-1.html

In the query, the firstNOW()function executed, and theSLEEP(5)function paused the execution of the query for 5 seconds, and the secondNOW()function executed. However, bothNOW()functions return the same value though they executed at the different times.

If you want to get exact time at which the statement executes, you need to useSYSDATE()instead; see the following example:SELECTSYSDATE(),SLEEP(5),SYSDATE();

article-114020-1.html

If you want to change the MySQL server’s time zone to adjust the current date and time returned by theNOW()function, you use the following statement:SETtime_zone=your_time_zone;

MySQL NOW() function calculations

Because theNOW()function returns a number when it is used in a numeric context, you can use it in calculations e.g., now plus 1 hour, now minus 1 hour, now plus 1 day and now minus in day, etc.

The following statement returns the current date and time, now minus 1 hour and now plus 1 hour:--mysqlnowminus1hourSELECT(NOW()-INTERVAL1HOUR)'NOW-1hour',NOW(),--mysqlnowplus1hourNOW()+INTERVAL1HOUR'NOW+1hour';

article-114020-1.html

The following statement returns the current date and time, now minus 1 day and now plus 1 day:--mysqlnowminus1daySELECT(NOW()-INTERVAL1DAY)'NOW-1day',NOW(),--mysqlnowplus1day(NOW()+INTERVAL1DAY)'NOW+1day';

article-114020-1.html

MySQL NOW() as a default value for a column

You can use theNOW()function as a default value for aDATETIMEorTIMESTAMPcolumn. When you omit the date or time value in theINSERT statement, MySQL inserts the current date and time into the column whose default value is NOW().

Let’s take a look at the following example.

First, create a new table namedtmpwith three columns:id,titleandcreated_on. Thecreated_oncolumn has default value specified by theNOW()function.CREATETABLEtmp(idINTPRIMARYKEYAUTO_INCREMENT,titleVARCHAR(255)NOTNULL,created_onDATETIMENOTNULLDEFAULTNOW()--orCURRENT_TIMESTAMP);

Notice thatCURRENT_TIMESTAMPandCURRENT_TIMESTAMP()are synonyms forNOW()so you can use them interchangeably.

Second, insert a new row into thetmptable without specifying the value for thecreated_oncolumn:INSERTINTOtmp(title)VALUES('TestNOW()function');

Third, query the data from thetmptable:SELECT*FROMtmp;

article-114020-1.html

The value of thecreated_oncolumn has been updated to the current date and time at which theINSERTstatement executed.

In this tutorial, we have introduced you to MySQLNOW()function that returns the current date and time at which the statement executed.

原文链接:http://outofmemory.cn/mysql/function/mysql-now

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: MySQL中的YEAR函数用于提取日期或日期/时间表达式中的年份部分。它的语法如下: YEAR(date) 其中date是一个日期或日期/时间表达式。 MySQL中的NOW函数用于返回当前日期和时间。它的语法如下: NOW() 它不需要任何参数,直接调用即可返回当前日期和时间。 ### 回答2: MySQL的YEAR()函数和NOW()函数是两个常用的日期和时间函数。 YEAR()函数用于提取日期中的年份。它的基本语法是: YEAR(date) 其中,date表示日期值或日期表达式。YEAR()函数返回一个整数,表示指定日期的年份。 例如,假设有一个名为orders的表,其中有一个名为order_date的日期列,存储了订单的日期。我们可以使用YEAR()函数来提取每个订单的年份,如下所示: SELECT order_date, YEAR(order_date) AS order_year FROM orders; 这样就可以得到每个订单的日期和年份。 而NOW()函数用于获取当前的日期和时间。它的基本语法是: NOW() 它返回一个DATETIME类型的值,表示当前的日期和时间。 例如,我们可以使用NOW()函数来插入一条新的记录,并将当前的日期和时间作为其中的一个列值,如下所示: INSERT INTO orders (order_date) VALUES (NOW()); 这样就可以将当前的日期和时间插入到订单表中的order_date列中。 总结起来,YEAR()函数用于提取日期的年份,而NOW()函数用于获取当前的日期和时间。它们都是MySQL中常用的日期和时间函数,提供了方便的日期和时间操作和处理能力。 ### 回答3: MySQL中的YEAR函数用于提取日期或日期时间值的年份部分。它接受一个日期或日期时间值作为参数,并返回一个整数值代表年份。 例如,使用YEAR函数提取日期值的年份: SELECT YEAR('2022-01-01'); 这将返回2022作为结果。 另外,YEAR函数也可以用在日期时间值中: SELECT YEAR('2022-01-01 12:34:56'); 这同样返回2022作为结果。 而MYSQL中的NOW函数用于返回当前日期和时间。它没有参数,直接调用即可。 例如,使用NOW函数获取当前日期和时间: SELECT NOW(); 这将返回类似于'2022-01-01 12:34:56'的结果。 总结起来,YEAR函数用于提取日期或日期时间值的年份部分,而NOW函数用于获取当前日期和时间。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值