Search code examples
phpmysqlsqlsql-optimization

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

Solution

  • 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;