当条件为空时,使查询结果等于零

时间:2010-03-22 19:13:07

标签: php mysql

我相信下面的查询应该有效。但是,当我运行它时,结果是空白的。我认为这种情况正在发生,因为目前,“评论”表是空的。所以没有s.submissionid = c.submissionid的实例。我想让下面的查询工作,即使没有s.submissionid等于c.submissionid。在这种情况下,我希望countComments等于零。

我该怎么做?

提前致谢,

约翰

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


$result = mysql_query($sqlStr);

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

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

您应该在命令行或通过phpMyAdmin / HeidiSQL等等运行查询。确保您获得正确的数据并且语法正确。

你缺少一些逗号,一个是在选择行上的s.submissionid之后,再一次是逐行的。

如果添加缺少的逗号,

a1ex07的查询应该有效。