简体   繁体   中英

SQL: How to select one row per day

I have a database that has a row entry for every minute (86,000 values per day) and I am trying to write PHP to select only one row per day. I have a column "timestamp" that has the current timestamp in regular format (2017-12-09 06:49:02).

Does anyone know how to write a select statement to do what I am trying to do?

Example output:

2017-12-09 06:49:02, datavalue
2017-12-10 06:49:02, datavalue
2017-12-11 06:49:02, datavalue
2017-12-12 06:49:02, datavalue

Here is one method:

select t.*
from t join
     (select min(t2.timestamp) as min_timestamp
      from t t2
      group by date(t2.timestamp)
     ) t2
     on t.timestamp = t2.min_timestamp;

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