I have a table in MySQL declared like this:
+------------+------------+------+-----+-------------------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+------------+------+-----+-------------------+-------+
| ts | timestamp | NO | | CURRENT_TIMESTAMP | |
| id_usuario | int(11) | YES | | NULL | |
| temp | float(3,1) | YES | | NULL | |
+------------+------------+------+-----+-------------------+-------+
And lets say this is the data stored in it:
+---------------------+------------+------+
| ts | id_usuario | temp |
+---------------------+------------+------+
| 2015-02-12 11:00:00 | 2 | 20.0 |
| 2015-02-15 10:00:00 | 2 | 21.0 |
| 2015-02-17 20:00:00 | 2 | 20.0 |
| 2015-02-18 11:00:00 | 1 | 23.5 |
| 2015-02-18 11:30:00 | 1 | 24.0 |
| 2015-02-19 12:00:00 | 1 | 23.5 |
| 2015-02-19 12:30:00 | 1 | 22.0 |
| 2015-02-19 15:00:00 | 1 | 23.5 |
| 2015-02-19 15:50:00 | 1 | 21.5 |
| 2015-02-20 14:00:00 | 2 | 27.0 |
| 2015-02-20 14:00:00 | 1 | 26.0 |
| 2015-02-20 21:54:33 | 2 | 22.0 |
+---------------------+------------+------+
And now... imagine you travel in time a day before, and you want to get the last input in the table. Then you travel a day before to do the same thing, and so on for the last 7 days... that's it! And then came up with a table like this one: (Note: i didn't travel in time!)
if "select now()" returns '2015-02-20 13:00:00', this procedure would return:
+---------------------+------------+------+
| ts | id_usuario | temp | <-- day 12 is out of range
+---------------------+------------+------+ but was the last input before
| 2015-02-12 11:00:00 | 2 | 20.0 | day 14 at 13hs. So its ok!
| 2015-02-15 10:00:00 | 2 | 21.0 | <-- day 17,16,15 before 13hs
| 2015-02-18 11:30:00 | 1 | 24.0 | <-- day 18 before 13hs
| 2015-02-19 12:30:00 | 1 | 22.0 | <-- day 19 before 13hs
| 2015-02-19 15:50:00 | 1 | 21.5 | <-- day 20 before 13hs
+---------------------+------------+------+
So the question is... how can I do it?