加入两个查询以显示在同一个中

时间:2014-10-18 19:44:22

标签: mysql sql

我有两个独立显示的查询。

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 个答案:

答案 0 :(得分: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';