PHP MySQL计数记录SELECT COUNT具有特定值和排序

时间:2016-08-08 16:59:23

标签: php mysql sorting

  

我有两张桌子:部门(20个部门)和门票(比如说   1000张门票)。每张票都分配给一个部门。我想   知道每个部门分配了多少张票。

[求助]感谢来自frz3993

的善意提示
  

为了完成带有工作结果的线程,在底部找到我的新脚本

我用这两个查询成功了。

前者加载部门。

对于后者,我使用SELECT COUNT表示当前部门的票数。

PHP

<?php
$mysqli = new mysqli("localhost", "root", "*****", "tickets");
$openticket = null;

/* check connection */
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}

$query = "SELECT id, name FROM dept ORDER BY id ASC"; // loads all the departmentes and their id

if ($result = $mysqli->query($query)) {

while ($row = $result->fetch_assoc()) {
    //echo $row["id"] . " " . $row["name"] . "<br>"; // test point 
    $sqlcounttickets = "SELECT COUNT(dept_id) FROM ticket WHERE (dept_id=" . $row["id"] . " AND status!=1)"; // count how manytickets for that department id , IF status 1, skip, since ticket is closed
    //echo $sqlcounttickets; // test point

    $result2 = $mysqli->query($sqlcounttickets); //execute second query and get the result of the SELECT COUNT

    //if ($mysqli->error) { //test point
    //    die($mysqli->error);
    //} // no errors

    $rowdue = $result2->fetch_row();
    if ($rowdue[0] > 0){
        echo "DeptLabelNum: " . $row["id"] . " - DeptName: " . $row["name"] . " " . $rowdue[0] ."<br>";
    }
    $openticket=$openticket+$rowdue[0];
}

/* free result set */
$result->free();
}
echo "<br>" . "Open Tickets: " . $openticket;

/* close connection */
$mysqli->close();
?>

输出显然未分类,因为部门的门票金额是随机的

DeptLabelNum: 0 - DeptName: Global (All Departments) 1
DeptLabelNum: 1 - DeptName: LCD 1
DeptLabelNum: 2 - DeptName: Smartphones 6
DeptLabelNum: 4 - DeptName: Pendrive 4
DeptLabelNum: 6 - DeptName: Plasma 7
DeptLabelNum: 22 - DeptName: HDD 1
DeptLabelNum: 23 - DeptName: Notebook 8
DeptLabelNum: 24 - DeptName: Tablet 12


Open Tickets: 40

你可以赌它:-),我想按降序对输出进行排序

所以平板电脑应该是第一张有12张票的人 第二本笔记本有8张票 第3次血浆

等等

您是否建议将周期的输出加载到MySQL临时表中?

或者您会使用PHP数组吗?

或者可以通过更有效的查询来完成?

感谢您提供任何帮助和建议,因为我对这三个中的任何一个感到困惑

R上。

  

P.S。解决方案 - 仅包含一个查询的新脚本   这是一个新的脚本,它将结果包含在html表中。

PHP     

/* check connection */
if ($mysqli->connect_errno) {
    printf("Connect failed: %s\n", $mysqli->connect_error);
    exit();
}

echo '<table>'."\xA";

$query = "
SELECT COUNT( ticket.id ) AS ticket_count, dept.id, dept.name
FROM ticket
LEFT JOIN dept ON ticket.dept_id = dept.id
WHERE ticket.status !=1
GROUP BY dept.id
ORDER BY ticket_count DESC";

if ($result = $mysqli->query($query)) {

    while ($row = $result->fetch_assoc()) {

        echo "\t" . "<tr><th>" . $row["name"] . "</th><th>" . $row["ticket_count"] . "</th></tr>". "\xA";

        $openticket=$openticket+$row["ticket_count"];
    }

    /* free result set */
    $result->free();
}

echo "\t" . "<tr><th></th><th></th></tr>". "\xA";

echo "\t" . "<tr><th>" . "Open Tickets: " . "</th><th>" . $openticket . "</th></tr>". "\xA";
echo "</table>". "\xA";

/* close connection */
$mysqli->close();
?>

1 个答案:

答案 0 :(得分:0)

您可以通过一个查询来完成。此外,为了确保您获得所有部门的列表,即使没有门票:

SELECT d.*,tmp.ticket_count FROM departments d
LEFT JOIN (SELECT count(*) AS ticket_count, department_id FROM tickets GROUP by department_id) tmp
ON d.id = tmp.department_id