tjc19999 Posted September 25, 2010 Share Posted September 25, 2010 Hello everyone, I need some help figuring out the proper subquery for my select statement. Here are 2 codes that I have: $result = mysql_query("SELECT * FROM applications WHERE history=(select MAX(history) FROM applications)"); $result = mysql_query("SELECT DISTINCT id FROM applications"); Both of these queries work, but what I need is a subquery that will 1) select DISTINCT id 2) for each distinct ID select max(history) Thank you in advance if you can help. Link to comment https://forums.phpfreaks.com/topic/214393-sub-query-help/ Share on other sites More sharing options...
kickstart Posted September 26, 2010 Share Posted September 26, 2010 Hi Something like this should do it SELECT * FROM applications a INNER JOIN (SELECT id, MAX(history) AS maxHistory FROM applications GROUP BY id) b ON a.id = b.id AND a.history = b.maxHistory All the best Keith Link to comment https://forums.phpfreaks.com/topic/214393-sub-query-help/#findComment-1115669 Share on other sites More sharing options...
tjc19999 Posted September 26, 2010 Author Share Posted September 26, 2010 That most certainly did the trick...a bit more complex than anything I have come across as of yet. Thank you very much...you have ended a long day of hitting my head against the desk. Link to comment https://forums.phpfreaks.com/topic/214393-sub-query-help/#findComment-1115675 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.