我有这两个查询几乎可以提供我需要的数据:
SELECT Products.ItemID, Products.ItemID + '-' + ModifierListItems.ItemID AS SizeItemID
FROM ModifierLists INNER JOIN
ProductModifierLists ON ModifierLists.ModifierListID = ProductModifierLists.ModifierListID INNER JOIN
ModifierListItems ON ModifierLists.ModifierListID = ModifierListItems.ModifierListID INNER JOIN
Products ON ProductModifierLists.ItemID = Products.ItemID AND ProductModifierLists.ManufacturerID = Products.ManufacturerID
WHERE (Products.ManufacturerID = 262) AND ModifierListName='Size'
ORDER BY Products.ItemID
SELECT Products.ItemID, ModifierListItems.ItemID AS ColorItemID
FROM ModifierLists INNER JOIN
ProductModifierLists ON ModifierLists.ModifierListID = ProductModifierLists.ModifierListID INNER JOIN
ModifierListItems ON ModifierLists.ModifierListID = ModifierListItems.ModifierListID INNER JOIN
Products ON ProductModifierLists.ItemID = Products.ItemID AND ProductModifierLists.ManufacturerID = Products.ManufacturerID
WHERE (Products.ManufacturerID = 262) AND ModifierListName='Color'
ORDER BY Products.ItemID
我正在寻找的最终输出是与ItemID上连接的ColorItemID连接的SizeItemID。
2个查询的示例结果:
ItemID SizeItemID
------- ----------
A A-S
A A-M
B B-M
B B-L
ItemID ColorItemID
------- -----------
A BLK
A WHT
B BLK
B WHT
B GRN
我正在寻找的结果如下:
FinalItemID
-----------
A-S-BLK
A-S-WHT
A-M-BLK
A-M-WHT
B-M-BLK
B-M-WHT
B-M-GRN
B-L-BLK
B-L-WHT
B-L-GRN
答案 0 :(得分:3)
您可以接受两个查询并将它们连接在一起以构建最终值。
在以下查询中,我还在表名上使用别名。许多人发现有意义的别名比长表名更容易阅读:
with tsize as (
SELECT p.ItemID,
p.ItemID + '-' + mli.ItemID AS SizeItemID
FROM ModifierLists ml INNER JOIN
ProductModifierLists pml
ON ml.ModifierListID = pml.ModifierListID INNER JOIN
ModifierListItems mli
ON ml.ModifierListID = mli.ModifierListID INNER JOIN
Products p
ON pml.ItemID = Products.ItemID AND
pml.ManufacturerID = p.ManufacturerID
WHERE (p.ManufacturerID = 262) AND ModifierListName='Size'
),
tcolor as (
SELECT p.ItemID, mli.ItemID AS ColorItemID
FROM ModifierLists moli INNER JOIN
ProductModifierLists pml
ON ml.ModifierListID = pml.ModifierListID INNER JOIN
ModifierListItems mli
ON ml.ModifierListID = mli.ModifierListID INNER JOIN
Products p
ON pml.ItemID = Products.ItemID AND
pml.ManufacturerID = p.ManufacturerID
WHERE (Products.ManufacturerID = 262) AND ModifierListName='Color'
)
select SizeItemID+'-'+ColorItemID
from tsize join tcolor
on tsize.itemid = tcolor.ItemID
答案 1 :(得分:2)
嗯,最干净的方法是将每个查询放入自己的视图中。它也可以帮助你理解你做得更好。因此,如果我们接受查询并创建了视图:
CREATE VIEW v_ProductSize
AS
SELECT Products.ItemID, Products.ItemID + '-' + ModifierListItems.ItemID AS SizeItemID
FROM ModifierLists INNER JOIN
ProductModifierLists ON ModifierLists.ModifierListID = ProductModifierLists.ModifierListID INNER JOIN
ModifierListItems ON ModifierLists.ModifierListID = ModifierListItems.ModifierListID INNER JOIN
Products ON ProductModifierLists.ItemID = Products.ItemID AND ProductModifierLists.ManufacturerID = Products.ManufacturerID
WHERE (Products.ManufacturerID = 262) AND ModifierListName='Size'
ORDER BY Products.ItemID
CREATE VIEW v_ProductColor
AS
SELECT Products.ItemID, ModifierListItems.ItemID AS ColorItemID
FROM ModifierLists INNER JOIN
ProductModifierLists ON ModifierLists.ModifierListID = ProductModifierLists.ModifierListID INNER JOIN
ModifierListItems ON ModifierLists.ModifierListID = ModifierListItems.ModifierListID INNER JOIN
Products ON ProductModifierLists.ItemID = Products.ItemID AND ProductModifierLists.ManufacturerID = Products.ManufacturerID
WHERE (Products.ManufacturerID = 262) AND ModifierListName='Color'
ORDER BY Products.ItemID
然后你有两个可以正常使用的简单视图,对吗?所以你的查询将是:
SELECT ps.SizeItemID +' - '+ pc.ColorItemID FROM v_ProductSize ps JOIN v_ProductColor pc ON ps.ItemID = pc.ItemID
看看它是如何工作的?你只是像任何其他表一样进行正常连接。现在,假设您不想创建视图,或者由于某种原因没有权限。您可以跳过视图部件本身,并使用子查询。您只需将参数替换为视图,查询本身就在括号中。
所以它看起来像这样:
SELECT ps.SizeItemID + '-' + pc.ColorItemID
FROM (
SELECT Products.ItemID, Products.ItemID + '-' + ModifierListItems.ItemID AS SizeItemID
FROM ModifierLists INNER JOIN
ProductModifierLists ON ModifierLists.ModifierListID = ProductModifierLists.ModifierListID INNER JOIN
ModifierListItems ON ModifierLists.ModifierListID = ModifierListItems.ModifierListID INNER JOIN
Products ON ProductModifierLists.ItemID = Products.ItemID AND ProductModifierLists.ManufacturerID = Products.ManufacturerID
WHERE (Products.ManufacturerID = 262) AND ModifierListName='Size'
ORDER BY Products.ItemID
) ps
JOIN
( SELECT Products.ItemID, ModifierListItems.ItemID AS ColorItemID
FROM ModifierLists INNER JOIN
ProductModifierLists ON ModifierLists.ModifierListID = ProductModifierLists.ModifierListID INNER JOIN
ModifierListItems ON ModifierLists.ModifierListID = ModifierListItems.ModifierListID INNER JOIN
Products ON ProductModifierLists.ItemID = Products.ItemID AND ProductModifierLists.ManufacturerID = Products.ManufacturerID
WHERE (Products.ManufacturerID = 262) AND ModifierListName='Color'
ORDER BY Products.ItemID
) pc ON ps.itemID=pc.ItemID
现在......所有这些,并且随着我的咖啡最终开始,您可以通过一行修改大大简化查询:
SELECT Products.ItemID, Products.ItemID + '-' + colors.ItemID + '-' + sizes.ItemID AS SizeColorItemID
FROM ModifierLists
INNER JOIN ProductModifierLists ON ModifierLists.ModifierListID = ProductModifierLists.ModifierListID
INNER JOIN ModifierListItems sizes ON ModifierLists.ModifierListID = ModifierListItems.ModifierListID
--Put modifier list name in join clause
AND ModifierListName='Size'
INNER JOIN ModifierListItems colors ON ModifierLists.ModifierListID = ModifierListItems.ModifierListID
--same here, but for color
AND ModifierListName = 'Color'
INNER JOIN Products ON ProductModifierLists.ItemID = Products.ItemID
AND ProductModifierLists.ManufacturerID = Products.ManufacturerID
WHERE (Products.ManufacturerID = 262) --Remove modifierlistitem here so you can use it in the join clauses
ORDER BY Products.ItemID
因为我们真的在原始查询中提取相同的信息,只有一个不同的条件......我们可以将modifierlistitem.name移动到join子句中,然后再次使用不同的条件连接到同一个表。然后我们根据我们使用的条件(大小,颜色)对表进行别名。这样你就可以引用别名并拉出正确的项ID,并立即将它们连接起来。
答案 2 :(得分:1)
试试这个:
您只需要将两个查询作为派生表并加入ItemID
Select a.SizeItemID+'-'+b.ColorItemID as FinalItemID
FROM
(SELECT Products.ItemID, Products.ItemID + '-' + ModifierListItems.ItemID AS SizeItemID
FROM ModifierLists INNER JOIN
ProductModifierLists ON ModifierLists.ModifierListID = ProductModifierLists.ModifierListID INNER JOIN
ModifierListItems ON ModifierLists.ModifierListID = ModifierListItems.ModifierListID INNER JOIN
Products ON ProductModifierLists.ItemID = Products.ItemID AND ProductModifierLists.ManufacturerID = Products.ManufacturerID
WHERE (Products.ManufacturerID = 262) AND ModifierListName='Size'
)a
JOIN
(SELECT Products.ItemID, ModifierListItems.ItemID AS ColorItemID
FROM ModifierLists INNER JOIN
ProductModifierLists ON ModifierLists.ModifierListID = ProductModifierLists.ModifierListID INNER JOIN
ModifierListItems ON ModifierLists.ModifierListID = ModifierListItems.ModifierListID INNER JOIN
Products ON ProductModifierLists.ItemID = Products.ItemID AND ProductModifierLists.ManufacturerID = Products.ManufacturerID
WHERE (Products.ManufacturerID = 262) AND ModifierListName='Color'
)b
on a.ItemID=b.ItemID
ORDER BY a.FinalItemID