在MySQL中,我使用以下三个表(它们的字段列在标题之后):
注释:
commentid loginid submissionid comment datecommented
登录:
loginid username password email actcode disabled activated created points
提交内容:
submissionid loginid title url displayurl datesubmitted
我想为给定的“用户名”显示“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>";
答案 0 :(得分:1)
您是否要显示某位用户的最近10条评论?在这种情况下,删除GROUP BY c.loginid
子句。您的where子句已经选择了该特定用户的注释。 Group by将尝试将它们聚合成一行。像s.url这样的聚合和细节值不会混合。
答案 1 :(得分:0)
如果我正确理解你的目标,你应该把表链接起来:
SELECT <yourfields>
FROM comment c
JOIN login l ON l.loginid = c.loginid
JOIN submission s ON s.submissionid = c.submissionid
WHERE l.username = ?