2010-05-17 14 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 = ?