Inner Join 和 Left Outer Join 如何一起使用?
How to use Inner Join and Left Outer Join together?
我有两个查询,我必须将第二个查询合并到第一个查询中,以便它仅获取货架上当前存在的那些项目的详细信息(该特定商店 ID 的所有部门)。
表格:
Item : item, uniqueLabel, ItemScale, ItemExpiry // columns can have duplicates
Description : uniqueLabel, itemName //fields have unique value
Store : StoreId, ShelfNo // fields have unique values
Department : ShelfNo, ItemNo ( There can be duplicate items in a single Shelf)
我需要将这两个功能合并为一个。
查询 1 以获取项目的详细信息。
select item, Description.itemName, ItemScale, ItemExpiry
from Item left outer join Description on (Item.uniqueLabel = Description.uniqueLabel)
where Item.uniqueLabel like '11%' //uniqueLabel starting from 11
order by Item.uniqueLabel
查询 2 以获取该商店货架(所有部门)中所有项目的详细信息。
select itemNo from Store, Department
where Store.ShelfNo = Department.ShelfNo
and Department.itemNo is not null and Store.StoreId in('12345')
我的努力:
select itemNo, Description.itemName, ItemScale, ItemExpiry
from Item, Description, Department
where Department.itemNo = Item.item
Item.uniqueLabel = Description.uniqueLabel
where Item.uniqueLabel like '11%'
但这会获取大量数据。
您的问题来自尝试对内部联接使用过时的逗号运算符。
如果您切换到查询 2 的“现代”(1992 年标准化)JOIN 语法,它将如下所示:
SELECT itemNo
FROM Store
INNER JOIN Department
ON Store.ShelfNo = Department.ShelfNo
WHERE Department.itemNo is not null
AND Store.StoreId in('12345')
然后您可以将 Item 添加为第二个 INNER JOIN,将其与部门相关联:
SELECT itemNo
FROM Store
INNER JOIN Department
ON Store.ShelfNo = Department.ShelfNo
INNER JOIN Item
ON Department.itemNo = Item.item
WHERE Department.itemNo is not null
AND Store.StoreId in('12345')
然后添加您的 LEFT OUTER JOIN 和来自查询 1 的额外 WHERE 条件:
SELECT itemNo, Description.itemName, ItemScale, ItemExpiry
FROM Store
INNER JOIN Department
ON Store.ShelfNo = Department.ShelfNo
INNER JOIN Item
ON Department.itemNo = Item.item
LEFT OUTER JOIN Description
ON Item.uniqueLabel = Description.uniqueLabel
WHERE Department.itemNo is not null
AND Store.StoreId in('12345')
AND Item.uniqueLabel like '11%'
该查询可能不是您想要的,并且合并查询后某些条件可能是多余的,但希望这表明显式连接语法如何混合 INNER 和 OUTER 连接。
我有两个查询,我必须将第二个查询合并到第一个查询中,以便它仅获取货架上当前存在的那些项目的详细信息(该特定商店 ID 的所有部门)。
表格:
Item : item, uniqueLabel, ItemScale, ItemExpiry // columns can have duplicates
Description : uniqueLabel, itemName //fields have unique value
Store : StoreId, ShelfNo // fields have unique values
Department : ShelfNo, ItemNo ( There can be duplicate items in a single Shelf)
我需要将这两个功能合并为一个。
查询 1 以获取项目的详细信息。
select item, Description.itemName, ItemScale, ItemExpiry
from Item left outer join Description on (Item.uniqueLabel = Description.uniqueLabel)
where Item.uniqueLabel like '11%' //uniqueLabel starting from 11
order by Item.uniqueLabel
查询 2 以获取该商店货架(所有部门)中所有项目的详细信息。
select itemNo from Store, Department
where Store.ShelfNo = Department.ShelfNo
and Department.itemNo is not null and Store.StoreId in('12345')
我的努力:
select itemNo, Description.itemName, ItemScale, ItemExpiry
from Item, Description, Department
where Department.itemNo = Item.item
Item.uniqueLabel = Description.uniqueLabel
where Item.uniqueLabel like '11%'
但这会获取大量数据。
您的问题来自尝试对内部联接使用过时的逗号运算符。
如果您切换到查询 2 的“现代”(1992 年标准化)JOIN 语法,它将如下所示:
SELECT itemNo
FROM Store
INNER JOIN Department
ON Store.ShelfNo = Department.ShelfNo
WHERE Department.itemNo is not null
AND Store.StoreId in('12345')
然后您可以将 Item 添加为第二个 INNER JOIN,将其与部门相关联:
SELECT itemNo
FROM Store
INNER JOIN Department
ON Store.ShelfNo = Department.ShelfNo
INNER JOIN Item
ON Department.itemNo = Item.item
WHERE Department.itemNo is not null
AND Store.StoreId in('12345')
然后添加您的 LEFT OUTER JOIN 和来自查询 1 的额外 WHERE 条件:
SELECT itemNo, Description.itemName, ItemScale, ItemExpiry
FROM Store
INNER JOIN Department
ON Store.ShelfNo = Department.ShelfNo
INNER JOIN Item
ON Department.itemNo = Item.item
LEFT OUTER JOIN Description
ON Item.uniqueLabel = Description.uniqueLabel
WHERE Department.itemNo is not null
AND Store.StoreId in('12345')
AND Item.uniqueLabel like '11%'
该查询可能不是您想要的,并且合并查询后某些条件可能是多余的,但希望这表明显式连接语法如何混合 INNER 和 OUTER 连接。