如何在1个查询中组合3个表

时间:2017-07-07 09:30:11

标签: php mysql

我需要在1个查询中组合3个东西,以在图表上表示它。

第一个查询是查找NetAmount2017 第二个查询是查找预算(2017年) 第三个查询是查找NetAmount2016

第一个查询:NetAmount2017

SELECT sum(a.netamt) as netamt2017, b.store_name 
FROM site_sales a JOIN site_store b ON b.storenum = a.storenum 
WHERE a.busidate >= '2017-01-01' AND a.busidate <='2017-04-30'
GROUP BY a.storenum

第二次质询:Budget2017

SELECT
SUM(CASE WHEN c.busidate BETWEEN '2017-01' AND '2017-04' THEN c.budget ELSE
0 END) as budget,
b.store_name
FROM site_kpimthslsbgt c JOIN site_store b ON b.storenum = c.storenum 
GROUP BY c.storenum

第三个查询:NetAmount2016(与第一个查询相同,但日期不同)

SELECT sum(a.netamt) as netamt2017, b.store_name 
FROM site_sales a JOIN site_store b ON b.storenum = a.storenum 
WHERE a.busidate >= '2016-01-01' AND a.busidate <='2016-04-30'
GROUP BY a.storenum

只要我只能组合2个查询,就不知道要结合3个查询。这是我对budget2017和netamount2017的组合。

 Select x.netamt, y.budget, y.store_name
 from
 (
 SELECT sum(a.netamt) as netamt, b.store_name 
 FROM site_sales a JOIN site_store b ON b.storenum = a.storenum 
 WHERE a.busidate >= '2017-01-01' AND a.busidate <='2017-04-30'
 GROUP BY a.storenum
 ) x
 inner join
 (
 SELECT
 SUM(CASE WHEN c.busidate BETWEEN '2017-01' AND '2017-04' THEN c.budget 
 ELSE 0 END) as budget,
 b.store_name
 FROM site_kpimthslsbgt c JOIN site_store b ON b.storenum = c.storenum 
 GROUP BY c.storenum
 ) y
 on x.store_name = y.store_name

我想要的结果应显示在包含3个条形图的1个图表中,即Netamount2017,Budget2017和Netamount2016。下图显示了结果,但2017年的预算仅显示1个月,而不是我从1月到4月选择的选定月份。这是我到目前为止收到的3个查询的查询

SELECT
SUM(CASE WHEN a.busidate BETWEEN '2016-01-01' AND '2016-04-30' THEN a.netamt 
ELSE 0 END) as netamt2016,
SUM(CASE WHEN a.busidate BETWEEN '2017-01-01' AND '2017-04-30' THEN a.netamt ELSE 0 
END) as netamt,
b.store_name,
c.budget
FROM site_sales a JOIN site_store b ON b.storenum = a.storenum 
JOIN site_kpimthslsbgt c ON b.storenum = c.storenum 
WHERE c.busidate = '2017-01' AND b.store_region='$store_region'
GROUP BY a.storenum

如果我在下面查询,请给我错误的计算。

SELECT
SUM(CASE WHEN a.busidate BETWEEN '2016-01-01' AND '2016-04-30' THEN a.netamt 
ELSE 0 END) as netamt2016,
SUM(CASE WHEN a.busidate BETWEEN '2017-01-01' AND '2017-04-30' THEN a.netamt ELSE 0 
END) as netamt,
b.store_name,
c.budget
FROM site_sales a JOIN site_store b ON b.storenum = a.storenum 
JOIN site_kpimthslsbgt c ON b.storenum = c.storenum 
WHERE c.busidate BETWEEN '2017-01' AND '2017-04' AND b.store_region='$store_region'
GROUP BY a.storenum

Wrong

1 个答案:

答案 0 :(得分:0)

感谢上帝!我找到了答案,将我的所有查询组合成1个查询。 这是代码:

 SELECT x.netamt2017,z.netamt2016, y.budget, y.store_name
 FROM
 (
 SELECT sum(a.netamt) as netamt2017, b.store_name 
 FROM site_sales a JOIN site_store b ON b.storenum = a.storenum 
 WHERE a.busidate >= '$date1' AND a.busidate <='$date2' AND 
 a.storenum='$storenum'
 GROUP BY a.storenum
 ) x

 inner join
 (
 SELECT
 SUM(CASE WHEN c.busidate BETWEEN '$date111' AND '$date211' THEN c.budget 
 ELSE 0 END) as budget,
 b.store_name
 FROM site_kpimthslsbgt c JOIN site_store b ON b.storenum = c.storenum 
 WHERE c.storenum='$storenum'
 GROUP BY c.storenum
 ) y
 on x.store_name = y.store_name
 INNER JOIN
 (
 SELECT sum(a.netamt) as netamt2016, b.store_name 
 FROM site_sales a JOIN site_store b ON b.storenum = a.storenum 
 WHERE a.busidate >= '$date1234' AND a.busidate <='$date2234' AND 
 a.storenum='$storenum'
 GROUP BY a.storenum
 ) z
 on y.store_name = z.store_name

Here is the final result