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

前端 未结 1 632
无人及你
无人及你 2021-01-27 17:23

I am using the following PHP script to grab and alter data from a MySQL table and print the results in a HTML table. I was hoping to order the data in ascending order by the

相关标签:
1条回答
  • 2021-01-27 17:32

    You can calculate it in the query, and then use it in 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";
    
    0 讨论(0)
提交回复
热议问题