
经常数据库的日期可以看到这么一大坨的数据,这个通常是bigint类型数据。
补充说明下数值类型:
![9MB]{5FCV1)SCT$6Q`(}K2C.png](https://img.haomeiwen.com/i711568/3ec1872f0451f518.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
Q1:接下来问题来,如果告诉你要2017-04-05的数据怎么查呢?
正解:
select * from table
where DATE_FORMAT((select from_unixtime(create_time/1000)), '%Y-%m-%d') =DATE_FORMAT('2017-04-05','%Y-%m-%d')
Q2:再如查个20170331到20170401这段时间之间的数据?
select DATE_FORMAT((select FROM_UNIXTIME(create_time/1000)),'%Y-%m-%d %H:%m:%s')
from table
where DATE_FORMAT((SELECT FROM_UNIXTIME(create_time/1000)),'%Y-%m-%d')
BETWEEN DATE_FORMAT('2017-03-31','%Y-%m-%d') and DATE_FORMAT('2017-04-01','%Y-%m-%d')]
这个查法会包含20170401这一整天的数据
如果只要到20170401 00:00:00,那么可以把DATE_FORMAT('2017-04-01','%Y-%m-%d') 换成DATE_FORMAT('2017-03-31','%Y-%m-%d')
或者用另外一种查询就是先把20170331和20170401两个日期先用java Date.getTime()得到1490889600000,1490976000000(距1970计算时间)
select DATE_FORMAT((select FROM_UNIXTIME(create_time/1000)),'%Y-%m-%d')
from table where create_time >= '1490889600000' and create_time<'1490976000000'
![Uploading Paste_Image_795345.png . . .]
网友评论