I have a table like this:
// permlog
+----+----------+-------------+
| id | id_user | unix_time |
+----+----------+-------------+
| 1 | 2353 | 1339412843 |
| 2 | 2353 | 1339412864 |
| 3 | 5462 | 1339412894 |
| 4 | 3422 | 1339412899 |
| 5 | 3422 | 1339412906 |
| 6 | 2353 | 1339412906 |
| 7 | 7785 | 1339412951 |
| 8 | 2353 | 1339413640 |
| 9 | 5462 | 1339413621 |
| 10 | 5462 | 1339414490 |
| 11 | 2353 | 1339414923 |
| 12 | 2353 | 1339419901 |
| 13 | 8007 | 1339424860 |
| 14 | 7785 | 1339424822 |
| 15 | 2353 | 1339424902 |
| 16 | 2353 | 1466272801 |
| 17 | 2353 | 1466272805 |
+----+----------+-------------+
I need to select last row for specific user. I have two queries:
So here is expected result:
/* :id = 5462 */
| 10 | 5462 | 1339414490 |
Using order by
:
SELECT * FROM permlog WHERE id_user = :id ORDER BY unix_time DESC LIMIT 0, 1;
Using two conditions on where
clause:
SELECT *
FROM permlog
WHERE id_user = :id AND unix_time = ( SELECT MAX(unix_time) FROM permlog WHERE id_user = :id);
Which one is better? Or is there a better third option?
Aucun commentaire:
Enregistrer un commentaire