我刚刚开始学习MySQL,我在遇到问题时从这个网站上学到了很多东西但我已经用这个打了一个砖墙1.我想为所有借记创建一个日志在我的3家商店进行的卡付款,按日期分组,每个商店有1列,最后是总列。通过我在这里找到的信息,我能够单独创建查询但不确定如何将它们组合成1个输出。
SELECT DATE(s.sale_time) AS Date, SUM(p.payment_amount) AS Total
FROM pos_sales_payments p
JOIN pos_sales s on p.sale_id = s.sale_id
WHERE p.payment_type = "Debit Card" AND s.sale_time > "2018-01-01 00:00:00" AND s.deleted = "0"
GROUP BY date(s.sale_time)
ORDER BY s.sale_time
SELECT DATE(s.sale_time) AS Date, SUM(p.payment_amount) AS Total
FROM pos_sales_payments p
JOIN pos_sales s on p.sale_id = s.sale_id
WHERE p.payment_type = "Debit Card" AND s.sale_time > "2018-01-01 00:00:00" AND s.deleted = "0" AND s.location_id = "1"
GROUP BY date(s.sale_time)
ORDER BY s.sale_time
答案 0 :(得分:1)
只需使用条件聚合:
SELECT DATE(s.sale_time) AS Date, SUM(p.payment_amount) AS Total,
SUM(CASE WHEN s.location_id = 1 THEN p.payment_amount ELSE 0 END) as Total_1
FROM pos_sales_payments p JOIN
pos_sales s
ON p.sale_id = s.sale_id
WHERE p.payment_type = 'Debit Card' AND
s.sale_time >= '2018-01-01' AND
s.deleted = '0'
GROUP BY date(s.sale_time)
ORDER BY s.sale_time;