hellouthere Posted March 2, 2007 Share Posted March 2, 2007 I am trying to take the most recent flightid for each callsign in the database... this is the query i have, adapted from the las one Barand gave me... $lstsql = "SELECT p.realname, h.IDPIREP, h.CreatedON FROM pilots AS p LEFT JOIN pirep AS h ON p.ID = h.IDPilot WHERE pos!='' GROUP BY p.realname ORDER BY p.ID"; this is after i print the table header... for ($i=0; $i<$number; $i++) { list($lstname, $lstid, $lstdate) = mysql_fetch_row($lstres); i ave a pirep table with IDPIREP, IDPilot and CreatedON fields... these are the only relevant ones... also a pilots table with ID (=IDPilot in pirep table) the query above returns the oldest result... Thanks in advance... Link to comment https://forums.phpfreaks.com/topic/40897-solved-another-display-problem-for-barand-again/ Share on other sites More sharing options...
Barand Posted March 2, 2007 Share Posted March 2, 2007 If IDPirep is an auto_increment field then the highest will be the latest. SELECT p.ID, p.realname, MAX(h.IDPirep) as latest FROM pilot p INNER JOIN pirep h ON p.ID = h.IDPilot GROUP BY p.ID Link to comment https://forums.phpfreaks.com/topic/40897-solved-another-display-problem-for-barand-again/#findComment-198115 Share on other sites More sharing options...
hellouthere Posted March 2, 2007 Author Share Posted March 2, 2007 could i then use for ($i=0; $i<$number; $i++) { $latest = mysql_result($lstres,$i, "latest"); to acess it... Link to comment https://forums.phpfreaks.com/topic/40897-solved-another-display-problem-for-barand-again/#findComment-198120 Share on other sites More sharing options...
Barand Posted March 2, 2007 Share Posted March 2, 2007 You could, but mysql_result is the slowest way to access a result set, mysql_fetch_row() is the fastest. <?php $lstsql = "SELECT p.ID, p.realname, MAX(h.IDPirep) as latest FROM pilot p INNER JOIN pirep h ON p.ID = h.IDPilot GROUP BY p.ID"; $lstres = mysql_query($lstsql) or die (mysql_error().'<p>$lstsql</p>'); while (list($id, $pilot, $latest) = mysql_fetch_row($lstres)) { echo "$id $pilot $latest <br/>"; } ?> Link to comment https://forums.phpfreaks.com/topic/40897-solved-another-display-problem-for-barand-again/#findComment-198138 Share on other sites More sharing options...
hellouthere Posted March 2, 2007 Author Share Posted March 2, 2007 thanks Barand, saved my life again... Link to comment https://forums.phpfreaks.com/topic/40897-solved-another-display-problem-for-barand-again/#findComment-198144 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.