简体   繁体   中英

PHP MySQL count records SELECT COUNT with a certain value and sort

I have two tables: departments (20 departments) and tickets (lets say 1000 tickets). Each ticket is assigned to one department. I wanted to know how many tickets are assigned to each department.

[SOLVED] thank to the kind tip from frz3993

for the sake of completing the thread with the working result, at bottom you find my new script

I succeeded in that with these two queries.

The former loads the departments.

For the latter, I have used SELECT COUNT for how many tickets for the current department.

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();
?>

the output is obviously unsorted since the tickets amount for department is random

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

You may bet on it :-) , I'd like to sort the output in descending order

So Tablet should be the first with 12 tickets second notebook with 8 tickets 3rd plasma

and so on

Do you suggest to load the output of the cycle into a MySQL temporary table?

Or would you use an PHP array?

Or it can be done with a more effective query?

Thank you for any help and suggestion since I am confuse with any of the three

R.

PS SOLUTION - the new script with one query only this is the new script which encloses in an html table the result.

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();
?>

You can do it by one query. Also, to be sure you get the list of all departments even there is no tickets for them:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM