如何将两个选择合并到不同的WHERE和特殊条件

时间:2015-11-17 18:23:35

标签: mysql sql inner-join

我有这样的表:

date|status|value

日期是日期, 状态为1表示挂起,2表示已确认 和值是订单的价值

我想获得3列:

date|#status pending|#status pending+confirmed

数据示例:

+------------+-----------------+-----------------+
| date       | status          | value           |
+------------+-----------------+-----------------+
| 2015-11-17 |              1  |               89|
| 2015-11-16 |              1  |               6 |
| 2015-11-16 |              2  |              16 |
| 2015-11-16 |              2  |              26 |
| 2015-11-15 |              2  |              26 |
| 2015-11-14 |              2  |              24 |
+------------+-----------------+-----------------+

我想要的例子:

+------------+-----------------+-----------------+
| date       | confirmed       |confirmed+pending|
+------------+-----------------+-----------------+
| 2015-11-17 |              0  |               1 |
| 2015-11-16 |              2  |               3 |
| 2015-11-15 |              1  |              1 |
| 2015-11-14 |              1  |              1 |
+------------+-----------------+-----------------+

我正在尝试:

    SELECT array1.DATE
        ,array1.confirmed
        ,array2.total
    FROM (
        SELECT DATE (DATE) AS DATE
            ,count(value) AS confirmed
        FROM Orders
        WHERE STATUS = '2'
        GROUP BY DATE (DATE) DESC limit 5
        ) AS array1
    INNER JOIN (
        SELECT DATE (DATE) AS DATE
            ,count(value) AS total
        FROM Orders
        GROUP BY DATE (DATE) DESC limit 5
        ) AS array2

但是我每个日期得到4个结果,重复确认值和不同的总交易。

如果我尝试分开,我可以得到两个正确的信息:

将仅列出过去5天的已确认订单总和:

SELECT array1.DATE
    ,array1.confirmed
    ,array2.total
FROM (
    SELECT DATE (DATE) AS DATE
        ,count(valor) AS confirmed
    FROM Orders
    WHERE STATUS = '2'
    GROUP BY DATE (DATE) DESC limit 5;
    )

将列出过去5天所有订单的总和:

SELECT DATE (DATE) AS DATE
    ,count(valor) AS total
FROM Orders
GROUP BY DATE (DATE) DESC limit 5

我观察到至少一个大问题:

有时我们会有一天有很多未确认的订单且零已确认,因此内部联接可能会失败。

3 个答案:

答案 0 :(得分:0)

您在ON中缺少INNER JOIN条款。或者,因为在您的情况下,您加入的列在两侧都是相同的,您可以使用USING

SELECT array1.DATE
    ,array1.confirmed
    ,array2.total
FROM (
    SELECT DATE (DATE) AS DATE
        ,count(value) AS confirmed
    FROM Orders
    WHERE STATUS = '2'
    GROUP BY DATE (DATE) DESC limit 5
    ) AS array1
INNER JOIN (
    SELECT DATE (DATE) AS DATE
        ,count(value) AS total
    FROM Orders
    GROUP BY DATE (DATE) DESC limit 5
    ) AS array2
USING (DATE)

答案 1 :(得分:0)

您可以使用CASE WHEN,为了获得预期的输出,您已经给出了。

SELECT `date`,
    (SUM(CASE WHEN `status`=1 THEN 1 ELSE 0 END)) AS Confirmed,
    (SUM(CASE WHEN `status`=1 OR `status`=2 THEN 1 ELSE 0 END)) AS Confirmed_Pending
    FROM
    table_name
    GROUP BY DATE(`date`) DESC 

希望这有帮助。

答案 2 :(得分:0)

更简单的方法是使用case表达式来评估状态是否是您想要计算的内容,并将count函数应用于此:

SELECT   DATE (`date`) AS `date`,
         COUNT(CASE status WHEN 2 THEN 1 END) AS `confirmed`,
         COUNT(CASE WHEN status IN (1, 2) THEN 1 END) AS `pending and confirmed`,
FROM     orders
GROUP BY DATE (`date`) DESC