unixtime时间转换mysql_mysql from_unixtime()函数,把整数型时间戳转换为日期格式

tcversions数据表中

creation_ts字段类型:是日期字符串应该转变为时间戳

mysql from_unixtime()函数:php提供值为时间戳(1502003361),mysql中存储为日期格式(2017-08-28)如何进行sql语句中的计算或比较

解决方案:以上两种就是在,进行时间比较或者计算时,使用mysql中的from_unixtime()函数,把整数型时间戳转换为日期格式,在通过日期格式在数据表中查询

上表中creation_ts字段值是日期格式

select a.author_id from tcversions a,(select max(id) as id,author_id from tcversions group by author_id) b where a.creation_ts >=1502003361 and a.creation_ts <= 1504681761 and a.id=b.id and a.author_id=b.author_id

php专递过来的sql语句中 a.creation_ts >=1502003361 and a.creation_ts <= 1504681761进行比较的日期是10位时间戳(整形),这就没法直接查询

改造办法:a.creation_ts >=from_unixtime(1502003361) and a.creation_ts <= from_unixtime(1504681761) ,就是使用了mysql中的from_unixtime()函数,先把时间戳转换为日期格式(字符串)在进行比较计算

改造后语句:

select a.author_id from tcversions a,(select max(id) as id,author_id from tcversions group by author_id) b where a.creation_ts >=from_unixtime(1502003361) and a.creation_ts <= from_unixtime(1504681761) and a.id=b.id and a.author_id=b.author_id

.....

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值