0

我在向该查询添加 where 子句时遇到问题。我希望它从“SAT AM/SUN PM”列是“是”和“已确认”列是“是”的位置中进行选择。

这在没有 'WHERE shift_times='SAT AM/SUN PM' 的情况下有效——它不会输出任何内容:

        $query = "SELECT position, COUNT(confirmed) FROM volConfirm WHERE shift_times='SAT AM/SUN PM' GROUP BY position"; 

    $result = mysql_query($query) or die(mysql_error());

    // Print out result
    while($row = mysql_fetch_array($result)){
        echo "There are ". $row['COUNT(confirmed)'] ." ". $row['position'] ." " . $row['shift_times'] . " volunteers.";
        echo "<br />";

更多信息:该表包含以“是”或“0”(表示否)确认的记录,并且 shift_times 是 SAT AM/SUN PM 或 SAT PM/SUN AM。有几个不同的位置。我正在尝试显示最终结果,例如:

有:“SAT AM/SUN PM”的“30”“艺术”志愿者

有:“SAT PM/SUN AM”“30”“艺术”志愿者

理想情况下,行会旋转,所以下面的回声将是“SAT PM/SUN AM”的逆数据——但这似乎有点棘手......

4

1 回答 1

3

I changed your select statement to select and group by the shift times, so one row would be selected per position, per shift time. I added an alias of 'cnt' to your count() and updated the php to use cnt in the echo statement No closing bracket at the end of your while loop (could be a copy and paste issue)

$query = "SELECT COUNT(confirmed) as cnt
          , position
          , shift_times 
         FROM volConfirm 
         WHERE confirmed='yes'  
         GROUP BY shift_times, position
         order by shift_times, position"; 

$result = mysql_query($query) or die(mysql_error());

// Print out result
while($row = mysql_fetch_array($result))
{
    echo "There are ". $row['cnt'] ." ". $row['position'] ." " . $row['shift_times'] . " volunteers.";
    echo "<br />";
}
于 2012-05-09T05:51:58.200 回答