I have one table with some data and I want select newest data for each type...
Table:
+----+------+------+---------------------+
| ID | data | type | date |
+----+------+------+---------------------+
| 1 | just | 2 | 2010-08-07 14:24:48 |
| 2 | some | 2 | 2010-08-07 18:07:32 |
| 3 | data | 9 | 2010-08-06 02:52:17 |
| 4 | abcd | 1 | 2010-08-08 17:23:22 |
| 5 | efg1 | 5 | 2010-07-10 21:36:55 |
| 6 | c123 | 5 | 2010-07-10 20:44:36 |
| 7 | bbey | 12 | 2010-08-09 09:01:26 |
+----+------+------+---------------------+
Currently I'm using simple subquery and looks like everything works
SELECT `data`,`type`,`date`
FROM `table1`
WHERE `date` = (
SELECT MAX( `date` )
FROM `table1` AS tbl2
WHERE tbl2.`type` = `table1`.`type`
)
GROUP BY `type`
ORDER BY `type`,`date`
Result:
+------+------+---------------------+
| data | type | date |
+------+------+---------------------+
| abcd | 1 | 2010-08-08 17:23:22 |
| some | 2 | 2010-08-07 18:07:32 |
| efg1 | 5 | 2010-07-10 21:36:55 |
| data | 9 | 2010-08-06 02:52:17 |
| bbey | 12 | 2010-08-09 09:01:26 |
+------+------+---------------------+
My question is is there a better way to do this, some optimization, improvement or maybe it's possible to make join?