hellouthere Posted March 31, 2007 Share Posted March 31, 2007 Hello again, Barand has recently given me a query for selecting the most recent record from a database... $lstsql = "SELECT p.pilot_id , p.name, MAX(r.report_id) AS latest FROM pilots AS p INNER JOIN reports AS r ON p.pilot_id = r.pilot_id WHERE p.pilot_id='$pid' GROUP BY p.name"; i now need to select the 5 most recent results... i have seen other ways of doing it but cannot reproduce them... any help is appreiciated... thanks in advance.... (also the correct spelling for appreiciated ) Link to comment https://forums.phpfreaks.com/topic/45061-solved-sql-query/ Share on other sites More sharing options...
shocker-z Posted March 31, 2007 Share Posted March 31, 2007 $lstsql = "SELECT p.pilot_id , p.name, MAX(r.report_id) AS latest FROM pilots AS p INNER JOIN reports AS r ON p.pilot_id = r.pilot_id WHERE p.pilot_id='$pid' GROUP BY p.name LIMIT 5"; That should do the trick Regards Liam Link to comment https://forums.phpfreaks.com/topic/45061-solved-sql-query/#findComment-218755 Share on other sites More sharing options...
hellouthere Posted March 31, 2007 Author Share Posted March 31, 2007 If there is only 1 row without the limit, how could applying a limit make more? Ive tested and it doesnt work... Any1 else? Link to comment https://forums.phpfreaks.com/topic/45061-solved-sql-query/#findComment-218762 Share on other sites More sharing options...
shocker-z Posted March 31, 2007 Share Posted March 31, 2007 sorry try this $lstsql = "SELECT TOP 5 p.pilot_id , p.name, MAX(r.report_id) AS latest FROM pilots AS p INNER JOIN reports AS r ON p.pilot_id = r.pilot_id GROUP BY p.name"; Liam Link to comment https://forums.phpfreaks.com/topic/45061-solved-sql-query/#findComment-218789 Share on other sites More sharing options...
obsidian Posted March 31, 2007 Share Posted March 31, 2007 I think this is what you're after: SELECT p.pilot_id , p.name, r.report_id AS latest FROM pilots AS p INNER JOIN reports AS r ON p.pilot_id = r.pilot_id WHERE p.pilot_id='$pid' ORDER BY r.report_id DESC GROUP BY p.name LIMIT 5; Link to comment https://forums.phpfreaks.com/topic/45061-solved-sql-query/#findComment-218793 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.