Üç Tablolar katılın

2 Cevap php

MySQL, ben aşağıdaki üç tablo (kendi alanları onların başlıklardan sonra listelenmiştir) kullanıyorum:

yorumum:

commentid loginid submissionid comment datecommented

login:

loginid username password email actcode disabled activated created points

gönderme:

submissionid loginid title url displayurl datesubmitted

Ben "username" $profile adında bir değişken eşittir ", adı" verilen bir için "datecommented" ve "yorum" görüntülemek istiyorum. Ben ayrıca "comment" bir köprü yapmak istiyorum 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"].' burada countComments eşittir COUNT(c.commentid) ve $rowc aşağıda listelenen sorgunun parçasıdır.

Ne istediğimi yapmak için aşağıdaki kodu kullanarak çalıştı, ama işe yaramadı. Nasıl ben o ben istiyorum ne yapmak değiştirebilir?

Teşekkür peşin,

John

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

2 Cevap

Belirli kullanıcı son 10 yorum göstermek demek? Bu durumda, GROUP BY c.loginid madde bırakın. Sizin burada yan zaten o kullanıcının yorum seçti. Tarafından grup bir satır onları toplamak için çalışacağız. S.url gibi agrega ve detay değeri karışmaz.

Ben doğru hedefe anlamak, sadece tabloları zincir olmalıdır:

SELECT <yourfields>
FROM comment c
JOIN login l ON l.loginid = c.loginid
JOIN submission s ON s.submissionid = c.submissionid
WHERE l.username = ?