简体   繁体   English

连接两个表。 Select 一个表中的所有行并且仅匹配另一表中的值?

[英]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.

相关问题 返回一个表中的所有行,而不管其他表中没有数据。 使用where子句 - Return all rows from one table regardless of no data in other tables. Using Where Clause 使用其他表中的数据更新一个表。 (加上) - Updating one table with data from other tables. (With addition) 根据另外两个表从一个表中选择值(关系) - Select values from one table based on two other tables (relational) 如何通过从左表中获取所有行并仅在右表中匹配来连接 2 个表 - How to join 2 tables with getting all rows from left table and only matching ones in right 如何在没有关系的情况下连接两个表,只从一个表中获取一个值并将其粘贴到另一个表中? - How to join two tables without a relationship, taking only one value from a table and pasting it to the other table? 我只想要桌子上的一行。 进行JOIN还是只进行两个选择调用比较好 - I only want one row from my tables. Is it better to do a JOIN or just make two select calls SQL连接具有不同行数的两个表从一个表中获取所有行 - SQL Join two tables with different number of rows get all rows from one table SQL 查询两个表,仅查看一个表中的非 null 值以导出匹配值 - SQL Query for two tables which only reviews non null values from one table to export matching values linq查询连接两个表并从另一个表中获取一个表值的计数 - linq query to join two tables and get the count from one table values from the other 仅通过与其他两个表的联接获取主表记录 - Get main table records only from join with two other tables
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM