[英]Join two tables. Select all rows from one table and only matching values from other table?
I have the following tables:我有以下表格:
Locations
+-------------+----------------+
| Location_ID | Location_Name |
+-------------+----------------+
| 1 | Administration |
| 2 | Parking |
| 3 | Warehouse |
| 4 | Shipping |
| 5 | Factory |
| 6 | Office |
| 7 | Processing |
+-------------+----------------+
Item_Quantity
+---------+-------------+-------------------+
| Item_ID | Location_ID | Location_Quantity |
+---------+-------------+-------------------+
| 1 | 3 | 10 |
| 1 | 5 | 50 |
| 2 | 3 | 7 |
+---------+-------------+-------------------+
I am trying to get a list of all Location_IDs and Location_Names with the Location_Quantity for a specified Item_ID.我正在尝试使用指定 Item_ID 的 Location_Quantity 获取所有 Location_ID 和 Location_Names 的列表。
The expected result for Item_ID = 1 would be this: Item_ID = 1 的预期结果是:
+-------------+----------------+-------------------+
| Location_ID | Location_Name | Location_Quantity |
+-------------+----------------+-------------------+
| 1 | Administration | 0 |
| 2 | Parking | 0 |
| 3 | Warehouse | 10 |
| 4 | Shipping | 0 |
| 5 | Factory | 50 |
| 6 | Office | 0 |
| 7 | Processing | 0 |
+-------------+----------------+-------------------+
The expected result for Item_ID = 2 would be this: Item_ID = 2 的预期结果是:
+-------------+----------------+-------------------+
| Location_ID | Location_Name | Location_Quantity |
+-------------+----------------+-------------------+
| 1 | Administration | 0 |
| 2 | Parking | 0 |
| 3 | Warehouse | 7 |
| 4 | Shipping | 0 |
| 5 | Factory | 0 |
| 6 | Office | 0 |
| 7 | Processing | 0 |
+-------------+----------------+-------------------+
I have tried the following queries:我尝试了以下查询:
SELECT l.Location_ID, l.Location_Name, iq.Location_Quantity
FROM Locations l
LEFT JOIN Item_Quantity iq ON l.Location_ID = iq.Location_ID
WHERE iq.Item_ID = @Item_ID
SELECT l.Location_ID, l.Location_Name, iq.Location_Quantity
FROM Item_Quantity iq
LEFT JOIN Locations l ON l.Location_ID = iq.Location_ID
WHERE iq.Item_ID = @Item_ID
SELECT l.Location_ID, l.Location_Name, Location_Quantity = iif(iq.Location_Quantity IS NOT NULL, iq.Location_Quantity, 0)
FROM Locations l
LEFT JOIN Item_Quantity iq ON l.Location_ID = iq.Location_ID
WHERE iq.Item_ID = @Item_ID
All queries return only the rows with entries in Item_Quantity.所有查询仅返回具有 Item_Quantity 条目的行。
This is what I am getting for Item_ID = 1 for any of the above queries:对于上述任何查询,这就是我为 Item_ID = 1 得到的结果:
+-------------+----------------+-------------------+
| Location_ID | Location_Name | Location_Quantity |
+-------------+----------------+-------------------+
| 3 | Warehouse | 10 |
| 5 | Factory | 50 |
+-------------+----------------+-------------------+
I would have thought a Left Join on the Locations table would give me all of the rows from the specified columns, but I must be understanding something incorrectly?我原以为 Locations 表上的左连接会给我指定列中的所有行,但我一定是理解错误?
Can anyone see what I am doing wrong here?谁能看到我在这里做错了什么?
The condition needs to go in the ON
clause.条件需要在ON
子句中为 go。 Otherwise, the WHERE
clause turns the outer join into an inner join.否则, WHERE
子句将外连接变为内连接。
You also want to convert the NULL
to a 0
, so use COALESCE()
:您还想将NULL
转换为0
,因此请使用COALESCE()
:
SELECT l.Location_ID, l.Location_Name, COALESCE(iq.Location_Quantity, 0) as Location_Quantity
FROM Locations l LEFT JOIN
Item_Quantity iq
ON l.Location_ID = iq.Location_ID AND iq.Item_ID = @Item_ID;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.