在一个foreach循环中合并2个单独的数据库查询,或将查询合并成一个

时间:2018-09-12 11:47:16

标签: php mysql join

我有这个查询来检查最近3个月的销售数字,还有另一个查询来检查退款数字。我想在表格中显示它们:

9月$ 100 $ 20
8月$ 200 $ 10
七月$ 150 $ 0

但是,我不确定将两个查询合并为一个查询是否更好。我试图在销售foreach循环中放置一个foreach循环以获取退款,但这导致6行而不是3行。

       // sales
        SELECT DATE_FORMAT(order_date, '%b') as month, SUM(order_total) as totalSales FROM
        (
           SELECT order_date, order_total
           FROM order_summary
           WHERE order_date <= NOW()
           and order_date >= Date_add(Now(),interval - 3 month)
           AND `order_status` = 'Approved'

           UNION ALL
           SELECT  Date_add(Now(),interval - 2 month)  AS order_date, 0 as order_total
           UNION ALL
           SELECT  Date_add(Now(),interval - 1 month)   AS order_date, 0 as order_total
           UNION ALL
           SELECT  Now() AS order_date, 0 as order_total
        ) as test
        GROUP BY DATE_FORMAT(order_date, '%m-%Y')
        ORDER BY DATE_FORMAT(order_date, '%m-%Y') DESC

      // refunds
    SELECT DATE_FORMAT(credit_date, '%b') as month, SUM(`credit_value`) as totalCredit FROM
        (
           SELECT credit_value, credit_date
           FROM credit
           WHERE credit_date <= NOW()
           and credit_date >= Date_add(Now(),interval - 3 month)
           AND `reason` = 'Refund'

           UNION ALL
           SELECT  Date_add(Now(),interval - 2 month)  AS credit_date, 0 as credit_value
           UNION ALL
           SELECT  Date_add(Now(),interval - 1 month)   AS credit_date, 0 as credit_value
           UNION ALL
           SELECT  Now() AS credit_date, 0 as credit_value
        ) as test
        GROUP BY DATE_FORMAT(credit_date, '%m-%Y')
        ORDER BY DATE_FORMAT(credit_date, '%m-%Y') DESC
        ");

在O. Jones回答后更新

 SELECT a.month, a.totalSales, b.totalCredit
 FROM (SELECT DATE_FORMAT(order_date, '%b') as month, SUM(order_total) as totalSales, DATE(DATE_FORMAT(order_date, '%Y-%m-01')) month_starting FROM
        (
           SELECT order_date, order_total
           FROM order_summary
           WHERE order_date <= NOW()
           and order_date >= Date_add(Now(),interval - 3 month)
           AND `order_status` = 'Approved'

           UNION ALL
           SELECT  Date_add(Now(),interval - 2 month)  AS order_date, 0 as order_total
           UNION ALL
           SELECT  Date_add(Now(),interval - 1 month)   AS order_date, 0 as order_total
           UNION ALL
           SELECT  Now() AS order_date, 0 as order_total
        ) as test
        GROUP BY DATE_FORMAT(order_date, '%m-%Y')
        ORDER BY DATE_FORMAT(order_date, '%m-%Y') DESC) a
 JOIN (         SELECT DATE_FORMAT(credit_date, '%b') as month, SUM(`credit_value`) as totalCredit, DATE(DATE_FORMAT(credit_date, '%Y-%m-01')) month_starting FROM
            (
               SELECT credit_value, credit_date
               FROM credit
               WHERE credit_date <= NOW()
               and credit_date >= Date_add(Now(),interval - 3 month)
               AND `reason` = 'Refund'

               UNION ALL
               SELECT  Date_add(Now(),interval - 2 month)  AS credit_date, 0 as credit_value
               UNION ALL
               SELECT  Date_add(Now(),interval - 1 month)   AS credit_date, 0 as credit_value
               UNION ALL
               SELECT  Now() AS credit_date, 0 as credit_value
            ) as test
            GROUP BY DATE_FORMAT(credit_date, '%m-%Y')
            ORDER BY DATE_FORMAT(credit_date, '%m-%Y') desc ) b ON a.month_starting = b.month_starting
ORDER BY a.month_starting

1 个答案:

答案 0 :(得分:0)

这是一项加入工作。您希望每个月显示一行,订单和信用额度的行。由于订单和抵免额来自不同的查询,因此您将它们合并在一起即可得到此信息。

首先将DATE(DATE_FORMAT(order_date, '%Y-%m-01')) month_starting添加到第一个查询的SELECT中,并将DATE(DATE_FORMAT(credit_date, '%Y-%m-01')) month_starting添加到第二个查询的SELECT。

然后用这样的JOIN将两个查询包装在一个外部查询中。

 SELECT a.month, a.totalSales, b.totalCredit
 FROM ( your first query ) a
 JOIN ( your second query ) b ON a.month_starting = b.month_starting
ORDER BY month_starting

诀窍是通过将它们包装在(query) alias

中来理解查询可以像表一样被使用