2010-04-05 57 views
0

我使用所謂的「登錄」一個MySQL表結構如下:查詢查找評論在一個表中,用戶名中的另一個表

loginid, username, password, email, actcode, disabled, activated, created, points 

我使用一種叫「註釋」 MySQL表與結構如下:

commentid, loginid, submissionid, comment, datecommented 

對於給定的「submisssionid」,我想打印出從表中「註釋」的以下信息:

-The領域的「意見」和「datecommented」。

同時,我想打印出從表「登陸」以下內容:

對應於「登錄ID」爲每排都排-The「用戶名」被從表中選擇「評論」。

我該怎麼做?

我試了下面的代碼,但它沒有奏效。

由於提前,

約翰

$submission = mysql_real_escape_string($_GET['submission']); 
$submissionid = mysql_real_escape_string($_GET['submissionid']); 


    $sqlStr = "SELECT 
        c.loginid 
        ,c.submissionid 
        ,c.comment 
        ,c.datecommented 
        ,l.username 
        ,COUNT(c.commentid) countComments 
       FROM 
        comment c 
       WHERE 
        c.submissionid = $submissionid 
       INNER 
       JOIN 
        login l 
        ON 
        c.loginid = l.loginid 
       GROUP 
        BY 
        c.submissionid 
       ORDER 
        BY 
        c.datecommented DESC 
       LIMIT 
        100";   

    $result = mysql_query($sqlStr); 

    $arr = array(); 
    echo "<table class=\"samplesrec\">"; 
    while ($row = mysql_fetch_array($result)) { 
     echo '<tr>'; 
     echo '<td class="sitename1">'.$row["comment"].'</td>'; 
     echo '</tr>'; 
     echo '<tr>'; 
     echo '<td class="sitename2"><a href="http://www...com/sandbox/members/index.php?profile='.$row["username"].'">'.$row["username"].'</a>'.$row["datecommented"].'</td>'; 
     echo '</tr>'; 
     } 
    echo "</table>";  

回答

0
SELECT comment.comment, comment.datecommented, login.username 
FROM comment 
LEFT JOIN login ON comment.loginid=login.loginid 
WHERE submissionid=$submissionid 
ORDER BY comment.datecommented DESC 
LIMIT 100"; 
相關問題