php - Ordering by two fields -
the input below sorts submissions timestamp field called "datesubmitted" in reverse chronological order. field in mysql table called "submission."
another mysql table "comment" has timestamp field called "datecommented."
each submission has 1 "datesubmitted" have several comments, each different "datecommented."
how sort submissions "datesubmitted" , each one's last "datecommented"? in other words, want top of list show either submitted entry or entry recent comment, whichever occurred most recently.
thanks in advance,
john
$sqlstr = "select s.loginid ,s.title ,s.url ,s.displayurl ,s.datesubmitted ,l.username ,s.submissionid ,count(c.commentid) countcomments submission s inner join login l on s.loginid = l.loginid left outer join comment c on s.submissionid = c.submissionid group s.submissionid order s.datesubmitted desc limit 10"; $tzfrom = new datetimezone('america/new_york'); $tzto = new datetimezone('america/phoenix'); // echo $dt->format(date_rfc822); $result = mysql_query($sqlstr); $arr = array(); echo "<table class=\"samplesrec\">"; while ($row = mysql_fetch_array($result)) { $dt = new datetime($row["datesubmitted"], $tzfrom); $dt->settimezone($tzto); echo '<tr>'; echo '<td class="sitename1"><a href="http://www.'.$row["url"].'" target="_blank">'.$row["title"].'</a> <div class="dispurl">'.$row["displayurl"].'</div></td>'; echo '</tr>'; echo '<tr>'; echo '<td class="sitename2name">submitted <a href="http://www...com/.../members/index.php?profile='.$row["username"].'">'.$row["username"].'</a> on '.$dt->format('f j, y &\nb\sp &\nb\sp g:i a').'</td>'; echo '</tr>'; echo '<tr>'; echo '<td class="sitename2"><a href="http://www...com/.../comments/index.php?submission='.$row["title"].'&submissionid='.$row["submissionid"].'&url='.$row["url"].'&countcomments='.$row["countcomments"].'&submittor='.$row["username"].'&submissiondate='.$row["datesubmitted"].'&dispurl='.$row["displayurl"].'">'.$row["countcomments"].' comments</a></td>'; echo '</tr>'; } echo "</table>";
select s.loginid, s.title, s.url, s.displayurl, s.datesubmitted, l.username, s.submissionid, count(c.commentid) countcomments, greatest(s.datesubmitted, coalesce(max(c.datecommented), s.datesubmitted)) most_recent submission s inner join login l on s.loginid = l.loginid left outer join comment c on s.submissionid = c.submissionid group s.submissionid order most_recent desc limit 10
Comments
Post a Comment