简体   繁体   中英

How to find last month's records from timestamp from MySQL?

In my database table timestamp column datatype is longtext like this 1486335600.

How to write a mysql select statement to retrieve last month information?

SELECT `timestamp` FROM `daily_deals`

Your timestamp are saved as unix timestamp, you can convert it to the date/time format with FROM_UNIXTIME():

select from_unixtime( 1486335600 )

last month information will be something like this:

select `timestamp`
from `daily_deals`
where
  `timestamp` >= unix_timestamp( current_date - interval 1 month )

but it depends on your requirements (eg you might want the whole January data now that we are in February, if that's the case we need to work a little on the where condition)

from_unixtim() this function convert your value in date foment like 2017-02-06 05:30:00 so you can simply can you date in where condition

     SELECT `timestamp` FROM `daily_deals` where  
FROM_UNIXTIME(timestamp) BETWEEN 2017-01-01 and 2017-01-31

using this you can get data between any two date

您应该尝试以下查询来检索上个月的信息。

SELECT `timestamp` FROM `daily_deals` WHERE date_created BETWEEN (CURRENT_DATE() - INTERVAL 1 MONTH) AND CURRENT_DATE();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM