[英]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). 我有两个表:部门(20个部门)和票证(比如说1000张票证)。 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 [已解决]感谢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. 对于后者,我使用SELECT COUNT
来显示当前部门的票数。
PHP 的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 因此平板电脑应该是第一张拥有12张票的笔记本电脑,第二张拥有8张票的笔记本电脑
and so on 等等
Do you suggest to load the output of the cycle into a MySQL temporary table? 您是否建议将循环的输出加载到MySQL临时表中?
Or would you use an PHP array? 还是您会使用PHP数组?
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. R.
PS SOLUTION - the new script with one query only this is the new script which encloses in an html table the result. PS解决方案-仅包含一个查询的新脚本,这是将结果包含在html表中的新脚本。
PHP 的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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.