如何使用 PHP 从 MySQL 查询中按升序排列值?
How to order values in ascending order from MySQL query with PHP?
我正在使用以下 PHP 脚本从 MySQL table 中获取和更改数据,并将结果打印在 HTML table 中。我希望通过由 $total_client_time / $total_available_time * 100
创建的 $utilization_percentage
变量按升序排列数据。这发生在 $sql
查询之后,所以我想知道这是否可以用 PHP 来完成?或者有没有办法改变 MySQL 查询以在查询中计算 $utilization_percentage
然后我可以 运行 查询中的 ORDER BY
子句?实现此目标的最佳方法是什么?
<?php
require_once 'tm-config.php';
// create connection
$conn = new mysqli($tmcurrentConfig['host'], $tmcurrentConfig['user'], $tmcurrentConfig['pass'], $tmcurrentConfig['name']);
// check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT SUM(t.available_time) AS total_available_time,
SUM(t.chargeable_time) AS total_chargeable_time,
SUM(t.admin_time) AS total_admin_time,
SUM(t.new_business_time) AS total_new_business_time,
c.name AS companyName
FROM Timesheet t
LEFT JOIN fos_user u ON(u.id = t.user_id)
LEFT JOIN company c ON(c.id = u.company_id)
GROUP BY u.company_id";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$total_available_time = $row["total_available_time"];
$total_chargeable_time = $row["total_chargeable_time"];
$total_admin_time = $row["total_admin_time"];
$total_new_business_time = $row["total_new_business_time"];
$total_client_time = $total_chargeable_time + $total_admin_time + $total_new_business_time;
$utilization = $total_client_time / $total_available_time;
$utilization_percentage = $utilization * 100;
$company_name = $row["companyName"];
// echo data into HTML table
echo
"<tbody>" . "<tr>" . "<td>" . $company_name . "</td>" . "<td>" . round($utilization_percentage) . " %" . "</td>" . "</tr>" . "</tbody>";
}
} else {
echo "No results";
}
$conn->close();
你可以在查询中计算出来,然后在ORDER BY
中使用。
$sql = "SELECT SUM(t.available_time) AS total_available_time,
SUM(t.chargeable_time) AS total_chargeable_time,
SUM(t.admin_time) AS total_admin_time,
SUM(t.new_business_time) AS total_new_business_time,
c.name AS companyName,
(SUM(t.chargeable_time) + SUM(t.admin_time) + SUM(t.new_business_time)) / SUM(t.available_time) * 100 AS utilization_percentage
FROM Timesheet t
LEFT JOIN fos_user u ON(u.id = t.user_id)
LEFT JOIN company c ON(c.id = u.company_id)
GROUP BY u.company_id
ORDER BY utilization_percentage";
我正在使用以下 PHP 脚本从 MySQL table 中获取和更改数据,并将结果打印在 HTML table 中。我希望通过由 $total_client_time / $total_available_time * 100
创建的 $utilization_percentage
变量按升序排列数据。这发生在 $sql
查询之后,所以我想知道这是否可以用 PHP 来完成?或者有没有办法改变 MySQL 查询以在查询中计算 $utilization_percentage
然后我可以 运行 查询中的 ORDER BY
子句?实现此目标的最佳方法是什么?
<?php
require_once 'tm-config.php';
// create connection
$conn = new mysqli($tmcurrentConfig['host'], $tmcurrentConfig['user'], $tmcurrentConfig['pass'], $tmcurrentConfig['name']);
// check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT SUM(t.available_time) AS total_available_time,
SUM(t.chargeable_time) AS total_chargeable_time,
SUM(t.admin_time) AS total_admin_time,
SUM(t.new_business_time) AS total_new_business_time,
c.name AS companyName
FROM Timesheet t
LEFT JOIN fos_user u ON(u.id = t.user_id)
LEFT JOIN company c ON(c.id = u.company_id)
GROUP BY u.company_id";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$total_available_time = $row["total_available_time"];
$total_chargeable_time = $row["total_chargeable_time"];
$total_admin_time = $row["total_admin_time"];
$total_new_business_time = $row["total_new_business_time"];
$total_client_time = $total_chargeable_time + $total_admin_time + $total_new_business_time;
$utilization = $total_client_time / $total_available_time;
$utilization_percentage = $utilization * 100;
$company_name = $row["companyName"];
// echo data into HTML table
echo
"<tbody>" . "<tr>" . "<td>" . $company_name . "</td>" . "<td>" . round($utilization_percentage) . " %" . "</td>" . "</tr>" . "</tbody>";
}
} else {
echo "No results";
}
$conn->close();
你可以在查询中计算出来,然后在ORDER BY
中使用。
$sql = "SELECT SUM(t.available_time) AS total_available_time,
SUM(t.chargeable_time) AS total_chargeable_time,
SUM(t.admin_time) AS total_admin_time,
SUM(t.new_business_time) AS total_new_business_time,
c.name AS companyName,
(SUM(t.chargeable_time) + SUM(t.admin_time) + SUM(t.new_business_time)) / SUM(t.available_time) * 100 AS utilization_percentage
FROM Timesheet t
LEFT JOIN fos_user u ON(u.id = t.user_id)
LEFT JOIN company c ON(c.id = u.company_id)
GROUP BY u.company_id
ORDER BY utilization_percentage";