关于数据库里的时间存储
我询问了许多前辈,最后整理了一份笔记出来,以防止自己忘记
1、数据库时间存储建议int(10)存储时间戳
转换方便,也没有时间区影响等,后面的读取数据分析性能高。
2、怎么查找时间戳格式的数据库某月份数据
如:SELECT count(*) FROM `order` WHERE FROM_UNIXTIME(add_time,'%Y-%m')='2019-06';
上面一条数据查找了6月份的所有数据
3、怎样用日期格式的数据库查找某月的数据
SELECT count(*) FROM `orders` WHERE DATE_FORMAT(add_time,'%Y-%m')=DATE_FORMAT('2019-05-01','%Y-%m');
查找5月的数据
最后送上一份参考
时间转字符串
select date_format(now(), ‘%Y-%m-%d’);
#结果:2016-01-05
时间转时间戳
select unix_timestamp(now());
#结果:1452001082
字符串转时间
select str_to_date(‘2016-01-02’, ‘%Y-%m-%d %H’);
#结果:2016-01-02 00:00:00
字符串转时间戳
select unix_timestamp(‘2016-01-02’);
#结果:1451664000
时间戳转时间
select from_unixtime(1451997924);
#结果:2016-01-05 20:45:24
时间戳转字符串
select from_unixtime(1451997924,’%Y-%d’);
//结果:2016-01-05 20:45:24
原文:https://blog.csdn.net/wr_edu/article/details/83014209