2014-10-18 53 views
1

我有兩個獨立顯示的查詢。加入兩個查詢以顯示在同一個

SELECT 'New Orders' AS 'Type', @new_customers AS 'Total', CONCAT(ROUND((@new_customers /@total_purchases) * 100, 1), '%') AS 'Percentage'; 
SELECT 'Repeat Orders' AS 'Type', @repeat_customers AS 'Total', CONCAT(ROUND((@repeat_customers/@total_purchases) * 100, 1), '%') AS 'Percentage'; 

我想一起列出他們所以表現出這樣

+---------------+-------+------------+ 
| Type   | Total | Percentage | 
+---------------+-------+------------+ 
| New Orders |  4 | 11.4%  | 
| Repeat Orders |  4 | 11.4%  | 
+---------------+-------+------------+ 

下面是變量和視圖

## SQL Variables 
SELECT COUNT(DISTINCT customer_email) FROM sales_flat_order INTO @new_customers; 
SELECT COUNT(customer_email) FROM view_orders WHERE total_orders > 1 INTO @repeat_customers; 

## VIEW Customer total orders 
CREATE VIEW foodhub_magento.v_orders AS 
    SELECT 
     customer_email, COUNT(*) AS total_orders 
    FROM 
     foodhubsales_flat_order 
    GROUP BY 
     customer_email 
    HAVING 
     COUNT(customer_email) > 1; 

回答

1

你可以只使用union all操作:

SELECT 'New Orders' AS 'Type', @new_customers AS 'Total', CONCAT(ROUND((@new_customers /@total_purchases) * 100, 1), '%') AS 'Percentage' 
UNION ALL 
SELECT 'Repeat Orders' AS 'Type', @repeat_customers AS 'Total', CONCAT(ROUND((@repeat_customers/@total_purchases) * 100, 1), '%') AS 'Percentage'; 
+0

太棒了,謝謝爲快速反應。加1並等待計時器放棄接受。 – ehime 2014-10-18 19:52:16