在MySQL的加入,我現在用的是以下三個表(其字段的標題後面列出):三個用表
評論:
commentid loginid submissionid comment datecommented
登錄:
loginid username password email actcode disabled activated created points
提交:
submissionid loginid title url displayurl datesubmitted
I想要爲給定的「用戶名」顯示「datecommented」和「comment」,其中「username」等於名爲$profile
的變量。我還想使「評論」超鏈接到http://www...com/.../comments/index.php?submission='.$rowc["title"].'&submissionid='.$rowc["submissionid"].'&url='.$rowc["url"].'&countcomments='.$rowc["countComments"].'&submittor='.$rowc["username"].'&submissiondate='.$rowc["datesubmitted"].'&dispurl='.$rowc["displayurl"].'
,其中countComments等於COUNT(c.commentid)
和$rowc
是下面列出的查詢的一部分。
我嘗試使用下面的代碼來做我想做的事,但它沒有奏效。我怎麼能改變它使它做我想要的?
由於提前,
約翰
$sqlStrc = "SELECT
s.loginid
,s.title
,s.url
,s.displayurl
,s.datesubmitted
,l.username
,l.loginid
,s.title
,s.submissionid
,c.comment
,c.datecommented
,COUNT(c.commentid) countComments
FROM
submission s
INNER
JOIN
login l
ON
s.loginid = l.loginid
LEFT OUTER
JOIN
comment c
ON
c.loginid = l.loginid
WHERE l.username = '$profile'
GROUP
BY
c.loginid
ORDER
BY
s.datecommented DESC
LIMIT
10";
$resultc = mysql_query($sqlStrc);
$arrc = array();
echo "<table class=\"samplesrec1c\">";
while ($rowc = mysql_fetch_array($resultc)) {
$dtc = new DateTime($rowc["datecommented"], $tzFromc);
$dtc->setTimezone($tzToc);
echo '<tr>';
echo '<td class="sitename3c">'.$dtc->format('F j, Y &\nb\sp &\nb\sp g:i a').'</a></td>';
echo '<td class="sitename1c"><a href="http://www...com/.../comments/index.php?submission='.$rowc["title"].'&submissionid='.$rowc["submissionid"].'&url='.$rowc["url"].'&countcomments='.$rowc["countComments"].'&submittor='.$rowc["username"].'&submissiondate='.$rowc["datesubmitted"].'&dispurl='.$rowc["displayurl"].'">'.stripslashes($rowc["comment"]).'</a></td>';
echo '</tr>';
}
echo "</table>";
附:我想你需要第二個查詢才能找到COUNT(c.commentid)。你不能在一個查詢中返回所有這些數據。 – 2010-05-17 22:46:23