PHP | How to sort an array in my case?

I have a query in a table which displays all the information about individual companies and their ranking, which formed a separate (second) request is from a completely different table.

$result = mysql_query("SELECT * FROM company",$link);

 If (mysql_num_rows($result) > 0)
{
 $company = mysql_fetch_array($result);
do
 { 

 $id_company = $company["id_с"];
 $query2 = mysql_query("SELECT id_company, sum( price + drc) FROM user WHERE id_company = $id_company group by id_company",$link);
 $result2 = mysql_fetch_array($query2);
 $rating = round(($result2["1"] * 100 / 5555),1);

 if($rating == ")
{
 $rating = 0;
}

echo '
<a href="company?id='.$company[" id_c"].'">'.$company["name_c"].' rating $rating</a>
';
}
 while($company = mysql_fetch_array($result));
 }


How to sort the first query by the second. There is a view in the variable $rating
July 9th 19 at 11:06
1 answer
July 9th 19 at 11:08
Solution
To rewrite everything in a single request. Something like:
SELECT `company`.`id_с`, `company`.`name_c`, ROUND(SUM(`price` + `drc`) * 100 / 5555, 1) AS `rating`
FROM `company` INNER JOIN `user` ON `user`.`id_company` = `company`.`id_с`
GROUP BY `company`.`id_с`
ORDER BY `rating`

Find more questions by tags MySQLPHP