连接两个表。选择一个表中的所有行,仅选择另一表中的值?

时间:2019-09-20 13:58:59

标签: sql sql-server tsql

我有下表:

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_Name的列表。

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

我本以为“位置”表上的“左连接”会给我指定列中的所有行,但我一定对理解有误?

有人在这里看到我在做什么错吗?

1 个答案:

答案 0 :(得分:1)

该条件需要放在params = ActiveSupport::HashWithIndifferentAccess.new() params['school_id'] = school.id params['id'] = standard_group.id params['categories_and_years'] = [ { category_id: category_1.id, year_ids: [ year_1.id ] } ] params = params.to_json let(:params) { params } 子句中。否则,ON子句会将外部联接转换为内部联接。

您还希望将WHERE转换为NULL,因此请使用0

COALESCE()