簡體   English   中英

連接兩個表。 Select 一個表中的所有行並且僅匹配另一表中的值?

[英]Join two tables. Select all rows from one table and only matching values from other table?

我有以下表格:

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 |
+---------+-------------+-------------------+

我正在嘗試使用指定 Item_ID 的 Location_Quantity 獲取所有 Location_ID 和 Location_Names 的列表。

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 |
+-------------+----------------+-------------------+

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 |
+-------------+----------------+-------------------+

我嘗試了以下查詢:

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

所有查詢僅返回具有 Item_Quantity 條目的行。

對於上述任何查詢,這就是我為 Item_ID = 1 得到的結果:

+-------------+----------------+-------------------+
| Location_ID | Location_Name  | Location_Quantity |
+-------------+----------------+-------------------+
|           3 | Warehouse      |                10 |
|           5 | Factory        |                50 |
+-------------+----------------+-------------------+

我原以為 Locations 表上的左連接會給我指定列中的所有行,但我一定是理解錯誤?

誰能看到我在這里做錯了什么?

條件需要在ON子句中為 go。 否則, WHERE子句將外連接變為內連接。

您還想將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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM