php - 按行估价计算答案排序结果

标签 php mysql sql

我有以下查询,为我提供了一个包含正确描述和数字的表格:

$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/

相关文章:

php - 在来自数据库的回显中显示文本区域格式

php - 从 Lightning .sqlite 中提取时间戳

sql - Postgresql:使用升序值更新 column1,具体取决于 column2 排序顺序

mysql - 如何在mysql表中存储记录更改

sql - 具有大量或未定义类别的交叉表

php - 向 joomla MVC ListView 中的每个项目添加相关数据的计数

php - 自动从 mysql 数据库填充数据并插入到新表中

PHP filter_input_array() 没有按预期失败

php - Json编码整个mysql结果集

java - (JDBC) 带有 if 语句和多个条件的 Mysql 查询