下面的输入按照反向时间顺序按照称为“datesubmitted”的时间戳字段对提交进行排序。该字段位于名为“提交”的MySQL表中。由两个字段排序
另一个MySQL表“comment”有另一个时间戳字段,称为“datecommented”。
每个提交只有一个“日期提交”,但它可能有几个评论,每个评论都有不同的“日期评论”。
我怎样才能通过“datesubmitted”和每个人的最后“日期评论”排序提交?换句话说,我希望此列表的顶部显示最近提交的条目或最近评论的条目,以最近发生的大部分为准。
由于提前,
约翰
$sqlStr = "SELECT
s.loginid
,s.title
,s.url
,s.displayurl
,s.datesubmitted
,l.username
,s.submissionid
,COUNT(c.commentid) countComments
FROM
submission s
INNER
JOIN
login l
ON
s.loginid = l.loginid
LEFT OUTER
JOIN
comment c
ON
s.submissionid = c.submissionid
GROUP
BY
s.submissionid
ORDER
BY
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 by <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>";
你错过了'LEFT'和'OUTER'之间的换行符。 ;-) – 2010-07-26 23:03:55