I'm trying to simplify this ...
SELECT * FROM `cats` inner join (select * from cats WHERE `warid` = 1 AND `color` = 'green' order by orderDate desc) a on (cats.id=a.id) group by cats.warId;
you can see here http://sqlfiddle.com/#!2/55f5b/5
but I'm not sure if this is better than your request, ... reading and acting from EXPLAIN still becomes my homework: D
ID SELECT_TYPE TABLE TYPE POSSIBLE_KEYS KEY KEY_LEN REF ROWS EXTRA 1 PRIMARY <derived2> ALL (null) (null) (null) (null) 2 Using temporary; Using filesort 1 PRIMARY cats eq_ref PRIMARY PRIMARY 4 a.id 1 2 DERIVED cats ALL (null) (null) (null) (null) 6 Using filesort
and adding an index will speed it up
source share