我哪里错了? SQL查询

时间:2010-10-23 15:30:34

标签: php mysql

$foot = mysql_query("SELECT count(*) 
                       FROM tblQA 
                      WHERE intResponseID = '' 
                        AND cCategory = 'Football' as qcount, 
                    (SELECT max(dPostDateTime) 
                       FROM tblQA 
                      WHERE intResponseID = '' 
                        AND cCategory = 'Football') as lastq");


$football = mysql_fetch_array($foot);

echo "<td class='forum'>" . $footbll['qcount'] . "</td>";
echo "<td class='forum'>" . $footbll['lastq'] . "</td>";

这不会在我的表格中显示任何内容。我没有发布整个HTML代码,我的表结构很好。

6 个答案:

答案 0 :(得分:3)

使用:

$foot = mysql_query("SELECT COUNT(*) AS qcount,
                            MAX(dPostDateTime) AS lastq
                      FROM tblQA 
                     WHERE intResponseID = '' 
                       AND cCategory = 'Football' ");

$football = mysql_fetch_array($foot);

echo "<td class='forum'>" . $football['qcount'] . "</td>";
echo "<td class='forum'>" . $football['lastq'] . "</td>";

我重写了你的查询,它可以在一个语句中完成。

答案 1 :(得分:1)

如果这是您的文字代码,则$footbll中会出现拼写错误。

答案 2 :(得分:0)

你应该有类似的东西:

while ($football = mysql_fetch_array($foot)) {
    echo "<td class='forum'>" . $football['qcount'] . "</td>";
    /* yadda yadda yadda */
}

您的代码中也有一些拼写错误。

答案 3 :(得分:0)

我认为你错过了,所在的组合器:...qcount, (SELECT...

答案 4 :(得分:0)

查询没有任何意义,我非常惊讶它会在您的数据库GUI中返回一些内容。

如果您不想更改语法,则必须执行以下操作:

SELECT (SELECT COUNT(*) FROM tblQA) AS qcount, (SELECT MAX(dPOstDateTime) FROM tblQA) AS lastq -- i didn't add the conditions

但是,通过执行此查询可以轻松完成:

SELECT COUNT(*) AS qcount,
       MAX(dPostDateTime) AS lastq
FROM tblQA
WHERE intResponseID = ''
    AND cCategory = 'Football'

请注意intResponseID听起来像是一个整数,你把它与空字符串比较似乎很奇怪......

答案 5 :(得分:0)

好的,这是我的解决方案。我弄清楚了:

$foot = mysql_query("SELECT count(*), max(dPostDateTime) FROM tblQA WHERE intResponseID = '' AND cCategory = 'Football'");

$football = mysql_fetch_assoc($foot);


echo "<td class='forum'><center>" . $football['count(*)'] . "</center></td>";
echo "<td class='forum'><center>" . $football['max(dPostDateTime)'] . "</center></td>";