带有union和join的SQL Query

时间:2012-01-11 12:54:32

标签: mysql union

我的设置就像这样

Table1: company_group - company_id, company_name
Table2: store         - store_id, store_name
Table3: sales         - sales_id, company_id, store_id, date, sales
Table4: wh_sales      - wh_sale_id, company_id, store_id, date, sales

Table5: purchase      - purchase_id, company_id, store_id, date, purchase

现在我可以使用select和union查询获取前四个表的数据,但是我无法弄清楚我应该使用哪个连接来获取同一个表中table5的数据

我正在使用前四个表的查询,如

select `company_group`.`company_name, `store`.`store_name`, MONTHNAME(date) AS MONTH,`sales`.`sales` 
from company_group, store,sales
where `company_group`.`company.id`=`sales`.`company.id`
and `store`.`store.id`=`sales`.`store.id`
group by company_name,store_name, 'MONTH'

UNION

select `company_group`.`company_name, `store`.`store_name`, MONTHNAME(date) AS MONTH,`wh_sales`.`sales`
from company_group, store,wh_sales
where `company_group`.`company.id`=`wh_sales`.`company.id`
and `store`.`store.id`=`wh_sales`.`store.id`
group by company_name,store_name, 'MONTH'

现在如何包含Table5以便我可以得到像

这样的结果
company_name store_name   month      sales    purchase
company-a     store-c    December    40000    45000

3 个答案:

答案 0 :(得分:0)

使用以下查询

select `company_name, `store_name`, MONTH, sum(`sales`) as sales, sum(purchase) as purchase   from (
select `company_group`.`company_name, `store`.`store_name`, MONTHNAME(date) AS MONTH,`sales`.`sales` , 0 as purchase
from company_group, store,sales
where `company_group`.`company.id`=`sales`.`company.id`
and `store`.`store.id`=`sales`.`store.id`

UNION

select `company_group`.`company_name, `store`.`store_name`, MONTHNAME(date) AS MONTH,`wh_sales`.`sales`, 0 as purchase
from company_group, store,wh_sales
where `company_group`.`company.id`=`wh_sales`.`company.id`
and `store`.`store.id`=`wh_sales`.`store.id`

UNION

select `company_group`.`company_name, `store`.`store_name`, MONTHNAME(date) AS MONTH, 0 as sales, purchase
from company_group, store,purchase
where `company_group`.`company.id`=`purchase`.`company.id`
and `store`.`store.id`=`purchase`.`store.id`) a
group by company_name,store_name, 'MONTH'

内部查询不需要group by子句,因为没有组函数。我已将其移至最外层的查询

答案 1 :(得分:0)

SELECT t1.`company_name`, t1.`store_name`,t1.`MONTH`,t1.`sales`, `purchase`.`purchase` FROM (

select `company_group`.`company_id`, `company_group`.`company_name, `store`.`store_name`, MONTHNAME(date) AS MONTH,`sales`.`sales` 
    from company_group, store,sales
    where `company_group`.`company.id`=`sales`.`company.id`
    and `store`.`store.id`=`sales`.`store.id`
    group by company_name,store_name, 'MONTH'

UNION

select `company_group`.`company_id`,`company_group`.`company_name, `store`.`store_name`, MONTHNAME(date) AS MONTH,`wh_sales`.`sales`
    from company_group, store,wh_sales
    where `company_group`.`company.id`=`wh_sales`.`company.id`
    and `store`.`store.id`=`wh_sales`.`store.id`
    group by company_name,store_name, 'MONTH') AS t1 

LEFT JOIN `purchase` 
ON t1.`company_id`=`purchase`.`company_id` 
AND t1.`store_id`=`purchase`.`store_id`

不确定这是否是专门针对MySQL的有效语法,但无论如何你应该得到这个想法。

UPD:重写了评论中提到的查询修复错误

答案 2 :(得分:0)

销售与整体&销售,显示两者的购买价格吧? 试试这个。

select `company_group`.`company_name, `store`.`store_name`, MONTHNAME(date) AS MONTH,`sales`.`sales`, `purchase`.`purchase`
from company_group, store,sales, purchase
where `company_group`.`company.id`=`sales`.`company.id`
and `store`.`store.id`=`sales`.`store.id`
group by company_name,store_name, 'MONTH'

UNION

select `company_group`.`company_name, `store`.`store_name`, MONTHNAME(date) AS MONTH,`wh_sales`.`sales`, `purchase`.`purchase`
from company_group, store,wh_sales, purchase
where `company_group`.`company.id`=`wh_sales`.`company.id`
and `store`.`store.id`=`wh_sales`.`store.id`
group by company_name,store_name, 'MONTH'

purchasepurchase添加到SELECT子句,将purchase TABLE添加到FROM子句,可以访问该表的purchase价格领域。根据您预计的表purchase列,最简单的方法是 AND