我有以下查询,为我提供了一个包含正确描述和数字的表格:
$result = mysqli_query($con,"SELECT * FROM b_sale_basket WHERE ORDER_ID=$ID");
$total=0;
while($row = mysqli_fetch_array($result))
{
$quantity = $row['QUANTITY'];
$description = $row['NAME'];
$unitprice = $row['PRICE'];
$lineprice = $row['PRICE']*$row['QUANTITY'];
$total=$total+$lineprice;
$tbl_header = '<table style="width: 650px;" cellspacing="0" cellpadding="5">';
$tbl_footer = '</table>';
$tbl = '';
$tbl .= '
<tr>
<td style="width: 50px; text-align: left; border-bottom: 0.1em solid #808080;"><p style="color:#808080;">'.number_format($quantity,0).'</p></td>
<td style="width: 350px; border-bottom: 0.1em solid #808080;"><p style="color:#808080;">'.$description.'</p></td>
<td style="width: 125px; text-align:right; border-bottom: 0.1em solid #808080;"><p style="color:#808080;">'.number_format($unitprice,2).'</p></td>
<td style="width: 125px; text-align:right; border-bottom: 0.1em solid #808080;" align="right" ><p style="color:#808080;">'.number_format($lineprice,2).'</p></td>
</tr>
';
正如您将看到的那样,$lineprice 的计算方式是:
$lineprice = $row['PRICE']*$row['QUANTITY'];
现在我想根据该字段中的值对表结果进行排序。我试过:
SELECT * FROM b_sale_basket WHERE ORDER_ID=$ID ORDER BY '$lineprice' ASC
但这行不通。如何按此列对表格中的结果进行排序?
先谢谢你, 安迪
最佳答案
ORDER BY PRICE * QUANTITY
按固定数字排序(就像您尝试的那样)是行不通的,因为数据库不知道要与哪个或哪些字段比较该固定值。但是,如果您在 deb 中进行乘法运算,如上面的代码片段所示,您将获得预期的排序顺序。
关于php - 按行估价计算答案排序结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19521945/