简体   繁体   English

如何使用PHP从MySQL查询中按升序对值进行排序?

[英]How to order values in ascending order from MySQL query with PHP?

I am using the following PHP script to grab and alter data from a MySQL table and print the results in a HTML table. 我正在使用以下PHP脚本从MySQL表中获取和更改数据,并将结果打印在HTML表中。 I was hoping to order the data in ascending order by the $utilization_percentage variable, which is created by $total_client_time / $total_available_time * 100 . 我希望通过$utilization_percentage变量按升序对数据进行排序,该变量由$total_client_time / $total_available_time * 100 This occurs after the $sql query, so I am wondering if this can be accomplished with PHP? 这是在$sql查询之后发生的,所以我想知道这是否可以用PHP完成? Or is there a way to alter the MySQL query to have the $utilization_percentage calculated within the query and then I can run a ORDER BY clause in the query? 还是有一种方法可以更改MySQL查询以在查询中计算$utilization_percentage ,然后在查询中运行ORDER BY子句? What would be the best way to accomplish this? 做到这一点的最佳方法是什么?

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

You can calculate it in the query, and then use it in ORDER BY . 您可以在查询中计算它,然后在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";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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