MySQL中时间函数NOW()和SYSDATE()的区别 以及多重字段查询写法

  mysql中日期函数还是比较常用的。主要有NOW()和SYSDATE()两种,虽然都表示当前时间,但使用上有一点点区别。

  NOW()取的是语句开始执行的时间,SYSDATE()取的是动态的实时时间。

因为NOW()取自mysql的一个变量”TIMESTAMP”,而这个变量在语句开始执行的时候就设定好了,因此在整个语句执行过程中都不会变化。

执行下面这个例子就明白了:

SELECT NOW(),SYSDATE(),SLEEP(3),NOW(),SYSDATE()

先查询了NOW()和SYSDATE(),然后sleep了3秒,再查询NOW()和SYSDATE(),结果如下:

 

NOW()还有3个同义词,效果跟NOW()一样,而且都有2种形式写法:

LOCALTIME或LOCALTIME()
LOCALTIMESTAMP或LOCALTIMESTAMP()
CURRENT_TIMESTAMP或CURRENT_TIMESTAMP()

 

select

id,

name,

(<include refid="salesCount"></include>) salesNum
from table

<sql id="salesCount">
select sum(op.all_num)
from shop_db.t_order_tour_package op ,shop_db.t_order st where t.id = op.tour_id
and st.id = op.order_id
and op.status = 1 and st.refund_status in (1,0)
</sql>
 
posted @ 2021-06-17 11:13  风骚羊肉串  阅读(332)  评论(0编辑  收藏  举报