MySQL:时间戳


#MySQL 笔记


MySQL 提供了一堆时间类型,但在我看来,还是直接用 int 存时间戳简洁、方便、易懂。

时间戳的可读性差,在直接使用SQL查询数据分析问题时很不方便。怎么办?用 from_unixtime 函数将其转换为年月日时分秒格式。

SQL 执行结果
select from_unixtime(1546698576); 2019-01-05 22:29:36
select from_unixtime(1546698576, '%Y-%m-%d %H:%i:%S'); 2019-01-05 22:29:36
select from_unixtime(1546698576, '%Y-%m-%d'); 2019-01-05

有没有办法将日期时间,转换为时间戳 ? 有,用unix_timestamp 函数。

SQL 执行结果
select unix_timestamp('2019-01-05 22:29:36'); 1546698576
select unix_timestamp('2019-01-05 00:00:00'); 1546617600
select unix_timestamp('2019-01-05'); 1546617600
select unix_timestamp('2019/01/05'); 1546617600
select unix_timestamp(now()); 当前的时间戳


( 本文完 )