SQL Query with union and join(带有联合和连接的 SQL 查询)
问题描述
我的设置是这样的
表1:company_group - company_id, company_name表 2:商店 - store_id、store_name表 3:销售额 - sales_id、company_id、store_id、日期、销售额表 4:wh_sales - wh_sale_id、company_id、store_id、日期、销售额表5:购买-purchase_id、company_id、store_id、日期、购买现在我可以使用 select 和 union 查询获取前四个表的数据,但是我无法弄清楚应该如何以及使用哪个连接来获取同一个表中 table5 的数据>
我正在使用前四个表的查询
选择`company_group`.`company_name,`store`.`store_name`, MONTHNAME(date) AS MONTH,`sales`.`sales`来自 company_group、商店、销售其中`company_group`.`company.id`=`sales`.`company.id`和`store`.`store.id`=`sales`.`store.id`按 company_name,store_name, 'MONTH' 分组联盟选择`company_group`.`company_name,`store`.`store_name`, MONTHNAME(date) AS MONTH,`wh_sales`.`sales`来自 company_group, store,wh_sales其中`company_group`.`company.id`=`wh_sales`.`company.id`和`store`.`store.id`=`wh_sales`.`store.id`按 company_name,store_name, 'MONTH' 分组现在如何包含 Table5 以便获得类似的结果
company_name store_name 月销售购买company-a store-c 十二月 40000 45000Sales vs Whole &销售,显示两者的购买价格对吗?试试这个.
选择`company_group`.`company_name, `store`.`store_name`, MONTHNAME(date) AS MONTH,`sales`.`sales`, `purchase`.`purchase`来自 company_group、商店、销售、购买其中`company_group`.`company.id`=`sales`.`company.id`和`store`.`store.id`=`sales`.`store.id`按 company_name,store_name, 'MONTH' 分组联盟选择`company_group`.`company_name,`store`.`store_name`, MONTHNAME(date) AS MONTH,`wh_sales`.`sales`, `purchase`.`purchase`来自 company_group、商店、wh_sales、购买其中`company_group`.`company.id`=`wh_sales`.`company.id`和`store`.`store.id`=`wh_sales`.`store.id`按 company_name,store_name, 'MONTH' 分组将 purchase.purchase 添加到 SELECT 子句和 purchase TABLE 到 FROM 子句提供对表的 purchase 价格字段的访问.其中,根据您的投影表的purchase 列是必不可少的AND 最简单的方法.
my set-up is like this
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
Now I am able to get the data for the first four tables using the select and union query, but I can't make out how and which join I should use to get the data for the table5 in the same table
I am using the query for the first four tables like
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'
now how can I include the Table5 so that I can get the result like
company_name store_name month sales purchase
company-a store-c December 40000 45000
Sales vs whole & sales, displaying the purchase price for both right ? Try this one.
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'
Adding purchase.purchase to the SELECT clause and the purchase TABLE to the FROM clause provides access to the table's purchase price field. Which, according to your projected table's purchase column is essential AND the simplest method.
这篇关于带有联合和连接的 SQL 查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:带有联合和连接的 SQL 查询
基础教程推荐
- MySQL根据从其他列分组的值,对两列之间的值进行求和 2022-01-01
- 带有WHERE子句的LAG()函数 2022-01-01
- 带更新的 sqlite CTE 2022-01-01
- 如何在 CakePHP 3 中实现 INSERT ON DUPLICATE KEY UPDATE aka upsert? 2021-01-01
- CHECKSUM 和 CHECKSUM_AGG:算法是什么? 2021-01-01
- 使用 VBS 和注册表来确定安装了哪个版本和 32 位 2021-01-01
- while 在触发器内循环以遍历 sql 中表的所有列 2022-01-01
- MySQL 5.7参照时间戳生成日期列 2022-01-01
- 从字符串 TSQL 中获取数字 2021-01-01
- ORA-01830:日期格式图片在转换整个输入字符串之前结束/选择日期查询的总和 2021-01-01
