2013-10-22 24 views
0

我有以下查詢給我提供了一個全面正確的描述和人物表:結果排序按行估值計算答案

$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 

但是這並不奏效。我怎麼能在這個專欄的表格中排列結果?

謝謝你在前進, 安迪

+2

'ORDER BY'需要列名,而不是列中包含的值。 –

+0

瞭解了,按這個數字排序還有嗎? – andy

+0

請參閱下面的Marc B的回答。 –

回答

5
ORDER BY PRICE * QUANTITY 

訂購固定數量,比如你想將無法正常工作,因爲DB會不知道哪一個領域(S)來比較固定價值反對。但是如果你在deb裏面進行乘法,就像在上面的代碼片段中一樣,你會得到你想要的排序順序。

+0

完美!我不敢相信我最初沒有嘗試過,但感謝你的幫助。 – andy

0
SELECT quantity * price as total FROM b_sale_basket ORDER BY total;