[英]SQL Data from multiple tables
I have four tables setup as follows我有四个表设置如下
Products产品
BranchData分支数据
BranchCostings分行成本核算
I am trying to create a query which will display a table as follows我正在尝试创建一个查询,它将显示如下表
with the following where clauses使用以下 where 子句
I have very limited SQL knowledge so am wondering how I would write a select query to select this data, I can get a table to display data from the product table and the Branch Data table but I am unable to get the Cost price for both branches from the Branch Costings table.我的 SQL 知识非常有限,所以想知道如何编写选择查询来选择此数据,我可以获得一个表来显示产品表和分支数据表中的数据,但我无法获得两个分支的成本价格来自 Branch Costings 表。
The SQL I am trying is as follows我正在尝试的 SQL 如下
Select P.ProductCode, P.ProductDescription, BC.CostPrice as B1Cost, BC.CostPrice as B2Cost
From Products P, BranchData BD, BranchCostings BC
Left Join BranchData on BC.BranchCosting = BD.BranchCosting
Where BD.BranchID = Branch1
I have been researching joins but this is where I am stuck, I did not know that you could join a table more than once and I am unsure which join to use (I think its a left join)我一直在研究连接,但这就是我被卡住的地方,我不知道你可以多次连接一个表,我不确定使用哪个连接(我认为它是一个左连接)
I have Just tried using the following which gives me all products in category 102 but it gives me these products for each cost price我刚刚尝试使用以下内容,它为我提供了类别 102 中的所有产品,但它为我提供了每种成本价格的这些产品
Select P.ProductCode, P.ProductDescription, BC.CostPrice as B1Cost, BC.CostPrice as B2Cost
From Products P, BranchCostings BC
Left Join Products as B0 on B0.BranchCosting = BC.BranchCosting
Left Join BranchData as B1 on B1.BranchCosting = BC.BranchCosting
Left Join BranchData as B2 on B2.BranchCosting = BC.BranchCosting
Where P.C_CATEGORY = 102
I now have the following which displays all the products in the category in the where statement but the B1CostPrice and the B2CostPrice are showing as NULL我现在有以下内容在 where 语句中显示类别中的所有产品,但 B1CostPrice 和 B2CostPrice 显示为 NULL
Select ProductCode, ProductDescription, B1.CostPrice, B2.CostPrice
From Products
left Join BranchData as B0 on Products.ProductCode = B0.ProductCode
left Join BranchCostings as B1 on B0.BranchCosting = B1.BranchCosting
left Join BranchCostings as B2 on B0.BranchCosting = B2.BranchCosting
Where C_CATEGORY = 102
I have tried changing the join types to see if this would display correctly but it either shows no data at all or shows the correct products with the Cost Price columns as NULL我尝试更改连接类型以查看这是否会正确显示,但它要么根本不显示数据,要么显示正确的产品,成本价格列为 NULL
Data Structure and expected table数据结构和预期表
Products产品
ProductCode ProductDescription Category
-----------------------------------------
Product1 Product Description 1 102
Product2 Product Description 2 102
Product3 Product Description 3 102
Product4 Product Description 4 102
Product5 Product Description 5 102
Product6 Product Description 6 99
BranchData分支数据
Product Code BranchID BranchCosting
Product1 B1 1
Product1 B2 2
Product2 B1 3
Product2 B2 4
Product3 B1 5
Product3 B2 6
Product4 B1 7
Product4 B2 8
Product5 B1 9
Product5 B2 10
Product6 B1 11
Product6 B2 12
BranchCostings分行成本核算
BranchCosting ListPrice CostPrice
1 2 1
2 4 1
3 6 3
4 8 4
5 10 5
6 12 6
7 14 7
8 16 7
9 18 9
10 20 10
11 22 11
12 24 12
Expected Table预期表
ProductCode ProductDescription B1CostPrice B2CostPrice
Product2 Product Description 2 3 4
Product3 Product Description 3 5 6
Product5 Product Description 5 9 10
Product6 Product Description 6 11 12
In the above table Product1 and Product 4 are not shown as the CostPrice is the same for both B1 and B2上表中未显示 Product1 和 Product 4,因为 B1 和 B2 的 CostPrice 相同
Ok I have finally figured this out.好吧,我终于想通了。
The select statement I used is as below我使用的选择语句如下
SELECT ProductCode, ProductDescription, B1.CostPrice as B1CostPrice, B2CostPrice as B2CostPrice
FROM Products
left JOIN BranchData as B0 on Products.ProductCode = B0.ProductCode
left JOIN BranchData as B9 on Products.ProductCode= B9.ProductCode
left join BranchCostings as B1 on B0.BranchCosting = B1.BranchCosting
left join BranchCostings as B2 on B9.BranchCosting = B2.BranchCosting
Where B0.Branch = Branch1 AND B9.Branch = Branch2 AND Products.Category = 102
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.