I tried to search on stackoverflow, google. I mostly see that we use max. But, on my records, it will be a thousand rows, so it would eat so much time before it get's finished.
Basically, I just want to get the latest record by datetime or unixtime.
The table records, sorted by datetime ASC:
Query: select * from accounts order by datetime ASC;
+----------+--------+---------------------+------------+
| user_id | status | datetime | unixtime |
+----------+--------+---------------------+------------+
| 14254047 | 1 | 2013-03-27 01:25:12 | 1364343912 |
| 14254270 | 1 | 2013-03-27 09:36:58 | 1364373418 |
| 14254619 | 1 | 2013-03-27 16:07:04 | 1364396824 |
| 14254047 | 0 | 2013-05-08 07:20:39 | 1367990439 |
| 14254270 | 0 | 2013-05-09 04:01:36 | 1368064896 |
| 14254619 | 0 | 2013-05-09 04:01:36 | 1368064896 |
+----------+--------+---------------------+------------+
When I grouped them by user_id, the result always show the first record. Not the latest, even adding order by datetime or unixtime DESC
Query: select * from accounts group by user_id order by datetime DESC;
+----------+--------+---------------------+------------+
| user_id | status | datetime | unixtime |
+----------+--------+---------------------+------------+
| 14254047 | 1 | 2013-03-27 01:25:12 | 1364343912 |
| 14254270 | 1 | 2013-03-27 09:36:58 | 1364373418 |
| 14254619 | 1 | 2013-03-27 16:07:04 | 1364396824 |
+----------+--------+---------------------+------------+
The result I wanted are the following:
+----------+--------+---------------------+------------+
| user_id | status | datetime | unixtime |
+----------+--------+---------------------+------------+
| 14254047 | 0 | 2013-05-08 07:20:39 | 1367990439 |
| 14254270 | 0 | 2013-05-09 04:01:36 | 1368064896 |
| 14254619 | 0 | 2013-05-09 04:01:36 | 1368064896 |
+----------+--------+---------------------+------------+
What could be the best approach, without using MAX()?
Regards