MySQL select based on daily timestamp range

2019-07-18 10:50发布

问题:

I am trying to select data from a table based on timestamps (stored as int(10)). The timestamps correspond to unix timestamps. I am using MySQL 5.

Question: How can I select the data based on the following rule: get all records, where the timestamp is from 9 to 10 o'clock. Nothing else. Timestamp could be in the 70's or today.

回答1:

If you're storing the value as a UNIX timestamp, you'll need to convert the value into MySQL's timestamp format before using the HOUR function.

WHERE HOUR(FROM_UNIXTIME(`timestamp`)) = 9


回答2:

Use HOUR() (assuming column is called timestamp)

WHERE HOUR(FROM_UNIXTIME(`timestamp`)) = 9