Join Query Not Working

Posted by John on Stack Overflow See other posts from Stack Overflow or by John
Published on 2010-05-31T13:14:02Z Indexed on 2010/05/31 13:42 UTC
Read the original article Hit count: 333

Filed under:
|

Hello,

I am using three MySQl tables:

comment

commentid loginid submissionid comment datecommented

login

loginid username password email actcode disabled activated created points

submission

submissionid loginid title url displayurl datesubmitted

In these three tables, the "loginid" correspond.

I would like to pull the top 10 loginids based on the number of "submissionid"s. I would like to display them in a 3-column HTML table that shows the "username" in the first column, the number of "submissionid"s in the second column, and the number of "commentid"s in the third column.

I tried using the query below but it did not work. Any idea why not?

Thanks in advance,

John

$sqlStr = "SELECT
                 l.username 
                 ,l.loginid  
                 ,c.commentid 
                 ,count(s.commentid) countComments
                 ,c.comment 
                 ,c.datecommented 
                 ,s.submissionid 
                 ,count(s.submissionid) countSubmissions
                 ,s.title
                 ,s.url 
                 ,s.displayurl 
                 ,s.datesubmitted
            FROM comment AS c
      INNER JOIN login AS l ON c.loginid = l.loginid
      INNER JOIN submission AS s ON c.loginid = s.loginid
        GROUP BY c.loginid
        ORDER BY countSubmissions DESC
           LIMIT 10";

  $result = mysql_query($sqlStr);

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

© Stack Overflow or respective owner

Related posts about php

Related posts about mysql