MySQL:MySQL日期数据实例、MySQL时候范例利用总结

MySQL:MySQL日期数据范例、MySQL时候范例利用总结

MySQL 日期范例:日期格局、所占存储空间、日期规模 比力。
日期范例 存储空间 日期格局 日期规模
———— ——— ——————— —————————————–
datetime 8 bytes YYYY-MM-DD HH:MM:SS 1000-01-01 00:00:00 ~ 9999-12-31 23:59:59
timestamp 4 bytes YYYY-MM-DD HH:MM:SS 1970-01-01 00:00:01 ~ 2038
date 3 bytes YYYY-MM-DD 1000-01-01 ~ 9999-12-31
year 1 bytes YYYY 1901 ~ 2155

在 MySQL 中建立表时,比较上面的表格,很轻易就能选择到符合本身的数据范例。不外到底是选择 datetime 仍是 timestamp,大概会有点犯难。这两个日期时候范例各有长处:datetime 的日期规模比力大;timestamp 所占存储空间比力小,只是 datetime 的一半。

别的,timestamp 范例的列另有个特征:默认环境下,在 insert, update 数据时,timestamp 列会主动以当前时候(CURRENT_TIMESTAMP)添补/更新。“主动”的意思便是,你不去管它,MySQL 会替你去向理。

一样平常环境下,我偏向于利用 datetime 日期范例。

MySQL 时候范例:时候格局、所占存储空间、时候规模。
时候范例 存储空间 时候格局 时候规模
———— ——— ——————— —————————————–
time 3 bytes HH:MM:SS -838:59:59 ~ 838:59:59

time 时候规模居然有这么大的规模,出格是 time 可以取负值,有点奇异。厥后,看了 MySQL 手册才知道这是为了知足两个日期时候相减才如许计划的。

select timediff(‘2000:01:31 23:59:59’, ‘2000:01:01 00:00:00’); — 743:59:59
select timediff(‘2000:01:01 00:00:00’, ‘2000:01:31 23:59:59’); — -743:59:59
select timediff(’23:59:59′, ’12:00:00′); — 11:59:59

注重,timediff 的两个参数只能是 datetime/timestamp, time 范例的,而且这两个参数范例要不异。即:datetime/timestamp 和 datetime/timestamp 比力;time 和 time 比拟较。

固然 MySQL 中的日期时候范例比力丰硕,但遗憾的是,今朝(2008-08-08)这些日期时候范例只能撑持到秒级别,不撑持毫秒、微秒。也没有发生毫秒的函数。

《MySQL:MySQL日期数据范例、MySQL时候范例利用总结》合用于 MySQL 5.X 及以上版本。

一、MySQL 得到当前日期时候 函数
1.1 得到当前日期+时候(date + time)函数:now()

mysql> select now();

+———————+
| now() |
+———————+
| 2008-08-08 22:20:46 |
+———————+

除了 now() 函数能得到当前的日期时候外,MySQL 中另有下面的函数:

current_timestamp()
,current_timestamp
,localtime()
,localtime
,localtimestamp — (v4.0.6)
,localtimestamp() — (v4.0.6)

这些日期时候函数,都等同于 now()。鉴于 now() 函数简短易记,发起老是利用 now() 来替换上面列出的函数。

1.2 得到当前日期+时候(date + time)函数:sysdate()

sysdate() 日期时候函数跟 now() 近似,分歧之处在于:now() 在实行起头时价就获得了, sysdate() 在函数实行时动态获得值。看下面的例子就大白了:

mysql> select now(), sleep(3), now();

+———————+———-+———————+
| now() | sleep(3) | now() |
+———————+———-+———————+
| 2008-08-08 22:28:21 | 0 | 2008-08-08 22:28:21 |
+———————+———-+———————+

mysql> select sysdate(), sleep(3), sysdate();

+———————+———-+———————+
| sysdate() | sleep(3) | sysdate() |
+———————+———-+———————+
| 2008-08-08 22:28:41 | 0 | 2008-08-08 22:28:44 |
+———————+———-+———————+

可以看到,固然半途 sleep 3 秒,但 now() 函数两次的时候值是不异的; sysdate() 函数两次获得的时候值相差 3 秒。MySQL Manual 中是如许形貌 sysdate() 的:Return the time at which the function executes。

sysdate() 日期时候函数,一样平常环境下很罕用到。

2. 得到当前日期(date)函数:curdate()

mysql> select curdate();

+————+
| curdate() |
+————+
| 2008-08-08 |
+————+

此中,下面的两个日期函数等同于 curdate():

current_date()
,current_date

3. 得到当前时候(time)函数:curtime()

mysql> select curtime();

+———–+
| curtime() |
+———–+
| 22:41:30 |
+———–+

此中,下面的两个时候函数等同于 curtime():

current_time()
,current_time

4. 得到当前 UTC 日期时候函数:utc_date(), utc_time(), utc_timestamp()

mysql> select utc_timestamp(), utc_date(), utc_time(), now()

+———————+————+————+———————+
| utc_timestamp() | utc_date() | utc_time() | now() |
+———————+————+————+———————+
| 2008-08-08 14:47:11 | 2008-08-08 | 14:47:11 | 2008-08-08 22:47:11 |
+———————+–

MySQL:MySQL日期数据实例、MySQL时候范例利用总结

相关文章:

你感兴趣的文章:

标签云: