在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