2011-02-15 82 views
0

這裏是查詢:如何計算產品的價格(ColdFusion的)名單的總和

<cfquery name="GET_SHIP_ROW" datasource="#DSN2#"> 
    SELECT 
     SR.*, 
     S.STOCK_CODE 
    FROM 
     SHIP_ROW SR, 
     #dsn3_alias#.STOCKS S 
    WHERE 
     SR.STOCK_ID = S.STOCK_ID AND 
     SR.SHIP_ID = #attributes.ship_id# 
    ORDER BY 
     SR.SHIP_ROW_ID 
</cfquery> 

,比如我有價格的循環代碼:

<cfloop from="#satir_start#" to="#satir_end#" index="i"> 
    <cfif i lte get_ship_row.recordcount> 
     <cfscript> 
      if(len(get_ship_row.discount[i]))indirim = get_ship_row.discount[i]; else indirim = 0; 
      adim_1 = get_ship_row.amount[i] * get_ship_row.price[i]; 
      adim_2 = (adim_1/100)*(100-indirim); 
      adim_3 = adim_2*(get_ship_row.tax[i]/100); 
      adim_4 = adim_2+adim_3; 
     </cfscript> 
     <cfquery name="GET_BARCODE" datasource="#DSN3#"> 
      SELECT 
       BARCOD 
      FROM 
       PRODUCT 
      WHERE 
       PRODUCT_ID = #get_ship_row.product_id[i]# 
     </cfquery> 
      <table> 
      <tr> 
      <td style="width:30mm;"><cfoutput>#get_ship_row.stock_code[i]#</cfoutput></td> 
     <td style="width:50mm;"><cfoutput>#left(get_ship_row.name_product[i],53)#</cfoutput></td> 
     <td style="width:25mm;" align="right"><cfoutput>#get_ship_row.amount[i]# #get_ship_row.unit[i]#</cfoutput></td> 
     <td style="width:25mm;" align="right"><cfoutput>#TLFormat(get_ship_row.price[i])#</cfoutput> TL</td> 
     <td style="width:35mm;" align="right"><cfoutput>#TLFormat(get_ship_row.amount[i] * get_ship_row.price[i])#</cfoutput> TL</td> 
     </tr> 
    </table>  
    </cfif> 
    </cfloop> 

顯示有所有我想要統計的名稱及其價格清單是它們的價格總和,我的意思是所有的產品。我該如何下手? thx求助!

回答

4

我只是根據你的(推測的)數據庫結構進行猜測。我建議將計算完全移到查詢中,以便db服務器正在完成最佳工作。在CF中操作這樣的東西並不是最優的。

你必須要測試這一點,但我認爲這是足夠接近上手:

<cfquery name="GET_SHIP_ROW" datasource="#DSN2#"> 
SELECT 
    SR.*, 
    S.STOCK_CODE, 
    ((sr.amount*sr.price) * ((100-discount)/100) * (tax/100)) as itemCost 
FROM 
    SHIP_ROW SR, 
    #dsn3_alias#.STOCKS S 
WHERE 
    SR.STOCK_ID = S.STOCK_ID AND 
    SR.SHIP_ID = #attributes.ship_id# 
ORDER BY 
    SR.SHIP_ROW_ID 
    </cfquery> 

一旦你在記錄行項總計,運行查詢的查詢來獲取的總和所有訂單項都非常簡單:

<cfquery name="total" dbtype="query"> 
select sum(itemCost) as shipmentTotal from GET_SHIP_ROW 
</cfquery> 

這有幫助嗎?

+0

我如何在代碼中插入它?喜歡這個? ` #TLFormat(get_ship_row.shipmentTotal)#`或像這樣` #TLFormat(total.shipmentTotal)#` – abrabr 2011-02-15 13:09:39