Hi Everyone:)
I'm creating this app where it's important to register if a person is active or not on the current day. My table structure looks like this:
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+-------------------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| kid_id | int(11) | NO | | NULL | |
| status | varchar(50) | NO | | NULL | |
| timestamp | timestamp | NO | | CURRENT_TIMESTAMP | |
What's important for me; is to select only values with timestamp equal to the current day. Next I need to check if the latest status is "active" or "inactive" of course based on kid_id. What I've figured out so far is this.
SELECT kid_id , status , timestamp FROM actions WHERE date(timestamp) = CURDATE() ORDER BY timestamp DESC;
Which returns these values:
+--------+----------+---------------------+
| kid_id | status | timestamp |
+--------+----------+---------------------+
| 4 | active | 2010-08-23 12:10:03 |
| 3 | inactive | 2010-08-23 10:53:18 |
| 3 | active | 2010-08-23 10:53:10 |
+--------+----------+---------------------+
Only problem now is that i receive both the active and inactive status with "kid_id" = "3". So my question is how do i only select the latest status from each kid_id.
In advance thank you very much for your help. It's appreciated.
---------- EDIT
I'll try to make my point a little more clear.
This is my table as it looks right now...
+--------+----------+---------------------+
| kid_id | status | timestamp |
+--------+----------+---------------------+
| 3 | inactive | 2010-08-23 18:32:19 |
| 4 | active | 2010-08-23 12:10:03 |
| 3 | active | 2010-08-23 10:53:10 |
+--------+----------+---------------------+
3 rows in set (0.00 sec)
I want to retrieve these values and only these
| 3 | inactive | 2010-08-23 18:32:19 |
| 4 | active | 2010-08-23 12:10:03 |
All of the solutions suggested below returns these values:
| 4 | active | 2010-08-23 12:10:03 |
| 3 | active | 2010-08-23 10:53:10 |
By the way... Thanks for all the responses so soon I'm really grateful for all the help.