0

我有这个 SQL 代码,它从表中选择行并按类别列将它们组合在一起

echo '<strong>Categories</strong><br>';
    $sql="SELECT * from tickets where deleted = '' and DAY(datetime) = '04' and MONTH(datetime) = '".date("m")."' and YEAR(datetime) = '".date("Y")."' group by category order by datetime ASC ";
    $rs=mysql_query($sql,$conn);
    while($result=mysql_fetch_array($rs))
    {
        echo $result["category"].' ('.mysql_num_rows($rs).')<br>';
    }

显示行数时,每行显示相同的数字。

我怎样才能让这个在每个“类别”旁边显示正确的数字?

4

2 回答 2

1
SELECT *, COUNT(category) AS my_groupcount from tickets

echo $result["category"].' ('.$result["my_groupcount"].')<br>';
于 2013-09-09T23:39:37.847 回答
0

您可能想要执行这样的查询,您正在使用该COUNT函数来告诉您每个类别分组中有多少票。

SELECT  category, COUNT(id) AS NumTickets, MIN(datetime) AS FirstDateTime
FROM tickets 
WHERE deleted = '' AND DAY(datetime) = 4 AND
        MONTH(datetime) = 12 AND YEAR(datetime) = '2012' 
GROUP BY category 
ORDER BY MIN(datetime)
于 2013-09-09T23:44:20.397 回答