简体   繁体   中英

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.

The expected result for Item_ID = 1 would be this:

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

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

This is what I am getting for Item_ID = 1 for any of the above queries:

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

Can anyone see what I am doing wrong here?

The condition needs to go in the ON clause. Otherwise, the WHERE clause turns the outer join into an inner join.

You also want to convert the NULL to a 0 , so use 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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM