SQL INNER JOIN 使用 CASE 和 3 个表
SQL INNER JOIN using CASE and 3 tables
我有以下表格,我只想显示可用的类型,同时计算该类别中的产品。
类型
Type_Id
Name
1
Candy
2
Chocolate Bar
产品
Product_Id
Name
Type_Id
AvailabilityId
1
Chocolate Name 1
1
1
2
Chcoolate Name 1
2
2
3
Candy Name 1
2
2
可用性
Availability_Id
Name
1
Available
2
Reserved
3
Sol d
期望的结果
Type_Id
Name
TotalAvailable
1
Candy
1
2
Chocolate
2
您可以使用带有内部联接的内部查询来实现此目的。这是 SQL fiddle: http://sqlfiddle.com/#!9/42bd7f/2
从基本的开始 JOIN between all three tables, on the related columns. Note use of table aliases t
- 类型,p
- 产品,a
- 可用性
SELECT *
FROM Types t
INNER JOIN Products p ON p.Type_Id = t.Type_Id
INNER JOIN Availability a ON a.Availability_Id = p.Availability_Id
接下来添加一个 WHERE 子句以按可用性状态过滤结果:
SELECT *
FROM Types t
INNER JOIN Products p ON p.Type_Id = t.Type_Id
INNER JOIN Availability a ON a.Availability_Id = p.Availability_Id
WHERE a.Name IN ('Available', 'Reserved')
结果:
Type_Id | Name | Product_Id | Name | Type_Id | Availability_Id | Availability_Id | Name
------: | :------------ | ---------: | :------------- | ------: | --------------: | --------------: | :--------
1 | Candy | 1 | Chocolate Name | 1 | 1 | 1 | Available
2 | Chocolate Bar | 2 | Chcoolate Name | 2 | 2 | 2 | Reserved
2 | Chocolate Bar | 3 | Candy Name | 2 | 2 | 2 | Reserved
最终 COUNT(*) 匹配的总行数,按类别分组(即 [Type].[Name]
)
SELECT t.Type_Id
, t.Name AS Type_Name
, COUNT(*) AS Total_Products
FROM Types t
INNER JOIN Products p ON p.Type_Id = t.Type_Id
INNER JOIN Availability a ON a.Availability_Id = p.Availability_Id
WHERE a.Name IN ('Available', 'Reserved')
GROUP BY t.Type_Id
, t.Name
Type_Id | Type_Name | Total_Products
------: | :------------ | -------------:
1 | Candy | 1
2 | Chocolate Bar | 2
我有以下表格,我只想显示可用的类型,同时计算该类别中的产品。
类型
Type_Id | Name |
---|---|
1 | Candy |
2 | Chocolate Bar |
产品
Product_Id | Name | Type_Id | AvailabilityId |
---|---|---|---|
1 | Chocolate Name 1 | 1 | 1 |
2 | Chcoolate Name 1 | 2 | 2 |
3 | Candy Name 1 | 2 | 2 |
可用性
Availability_Id | Name |
---|---|
1 | Available |
2 | Reserved |
3 | Sol d |
期望的结果
Type_Id | Name | TotalAvailable |
---|---|---|
1 | Candy | 1 |
2 | Chocolate | 2 |
您可以使用带有内部联接的内部查询来实现此目的。这是 SQL fiddle: http://sqlfiddle.com/#!9/42bd7f/2
从基本的开始 JOIN between all three tables, on the related columns. Note use of table aliases t
- 类型,p
- 产品,a
- 可用性
SELECT *
FROM Types t
INNER JOIN Products p ON p.Type_Id = t.Type_Id
INNER JOIN Availability a ON a.Availability_Id = p.Availability_Id
接下来添加一个 WHERE 子句以按可用性状态过滤结果:
SELECT *
FROM Types t
INNER JOIN Products p ON p.Type_Id = t.Type_Id
INNER JOIN Availability a ON a.Availability_Id = p.Availability_Id
WHERE a.Name IN ('Available', 'Reserved')
结果:
Type_Id | Name | Product_Id | Name | Type_Id | Availability_Id | Availability_Id | Name ------: | :------------ | ---------: | :------------- | ------: | --------------: | --------------: | :-------- 1 | Candy | 1 | Chocolate Name | 1 | 1 | 1 | Available 2 | Chocolate Bar | 2 | Chcoolate Name | 2 | 2 | 2 | Reserved 2 | Chocolate Bar | 3 | Candy Name | 2 | 2 | 2 | Reserved
最终 COUNT(*) 匹配的总行数,按类别分组(即 [Type].[Name]
)
SELECT t.Type_Id
, t.Name AS Type_Name
, COUNT(*) AS Total_Products
FROM Types t
INNER JOIN Products p ON p.Type_Id = t.Type_Id
INNER JOIN Availability a ON a.Availability_Id = p.Availability_Id
WHERE a.Name IN ('Available', 'Reserved')
GROUP BY t.Type_Id
, t.Name
Type_Id | Type_Name | Total_Products ------: | :------------ | -------------: 1 | Candy | 1 2 | Chocolate Bar | 2