2015-04-27 34 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”。请纠正错误的一个。 –

相关问题