2015-04-27 31 views
-1

我的連接查詢是在這裏,我想要在MySql查詢中的銷售價格原價的多少%?如何在MySql查詢中定義原始價格之間的銷售價格%?

SELECT DISTINCT (CASE 
     WHEN b.method = 'checkmo' THEN 'Check/Money order'  
     ELSE 'Credit Card (saved)' 
    END) AS 'Payment Method',a.increment_id as 'Ref-Order Number',a.store_name as 'purchased From (Store)', a.created_at as 'Purchased On',CONCAT (customer_firstname,' ',customer_lastname) as 'Sender-Store Name',CONCAT (customer_firstname,' ',customer_lastname) as 'Attention',c.telephone as 'Phone',c.street as 'Shipping Street',c.city as 'Shipping City',c.region as 'Shipping State',c.postcode as 'Shipping Zip',d.Sku as 'SKU',name as 'Name', round(d.base_price,2) as 'Sale Price', round(e.price,2) as 'Original Price', round(base_grand_total,2) as 'G.T.(Base)',round(grand_total,2) as 'G.T.(Purchased)',round(d.qty_ordered) as 'Product Qty Ordered',status as 'Status',a.coupon_code as 'Coupon Code',a.customer_email as 'Email' 
FROM sales_flat_order_item d left outer join sales_flat_order a on d.order_id = a.entity_id left outer join sales_flat_order_payment b on d.order_id = b.entity_id left outer join sales_flat_order_address c on a.customer_id = c.entity_id left outer join catalog_product_index_price 
e on d.product_id = e.entity_id where status != 'canceled' and d.base_price > 0 and e.customer_group_id = 0 

回答

1

我解決它通過添加round((e.price/d.base_price)*100) as 'Percentage'SELECT聲明:

SELECT DISTINCT (CASE 
     WHEN b.method = 'checkmo' THEN 'Check/Money order'  
     ELSE 'Credit Card (saved)' 
    END) AS 'Payment Method',a.increment_id as 'Ref-Order Number',a.store_name as 'purchased From (Store)', a.created_at as 'Purchased On',CONCAT (customer_firstname,' ',customer_lastname) as 'Sender-Store Name',CONCAT (customer_firstname,' ',customer_lastname) as 'Attention',c.telephone as 'Phone',c.street as 'Shipping Street',c.city as 'Shipping City',c.region as 'Shipping State',c.postcode as 'Shipping Zip',d.Sku as 'SKU',name as 'Name', round(e.price,2) as 'Original Price', round(d.base_price,2) as 'Sale Price', round((d.base_price/e.price)*100) as 'Percentage', round(base_grand_total,2) as 'G.T.(Base)',round(grand_total,2) as 'G.T.(Purchased)',round(d.qty_ordered) as 'Product Qty Ordered',status as 'Status',a.coupon_code as 'Coupon Code',a.customer_email as 'Email' 
FROM sales_flat_order_item d left outer join sales_flat_order a on d.order_id = a.entity_id left outer join sales_flat_order_payment b on d.order_id = b.entity_id left outer join sales_flat_order_address c on a.customer_id = c.entity_id left outer join catalog_product_index_price 
e on d.product_id = e.entity_id where status != 'canceled' and d.base_price > 0 and e.customer_group_id = 0 
+0

儘管此代碼塊可以回答這個問題,這將是最好的,如果你能提供它爲什麼這樣做一點解釋。 –

+0

@Pankaj:我編輯了你的答案,包括來自你的其他「答案」的代碼修復。我還添加了一些解釋性文字,以使您的回答可以理解。請使用將來您的帖子下方的[編輯]鏈接。如果我的編輯不是您想要的,您可以進一步編輯您的答案,甚至展開它。 –

+0

@Pankaj:您的文本顯示爲「e.price/d.base_price」,但您的代碼顯示爲「d.base_price/e.price」。請糾正錯誤的一個。 –

相關問題