2010-05-17 37 views
0

在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>"; 

回答

1

你的意思是展示某些用户在过去10个评论?在这种情况下,请删除GROUP BY c.loginid子句。您的where子句已经从该特定用户选择了评论。 Group by会尝试将它们聚合成一行。聚合和像s.url这样的细节值不会混合。

+0

附:我想你需要第二个查询才能找到COUNT(c.commentid)。你不能在一个查询中返回所有这些数据。 – 2010-05-17 22:46:23

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 = ?