好吧,最简洁的方式是将每个查询放入它自己的视图中。它还可以帮助您更好地了解您正在做的事情。因此,如果我们接受查询并创建视图:
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
那么你就有了两个可以正常使用的简单视图,对吧?所以你的查询是:
从 v_ProductSize ps 中选择 ps.SizeItemID + '-' + pc.ColorItemID 加入 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
因此,由于我们实际上在两个原始查询中提取了相同的信息,只有一个不同的条件……我们可以将修饰符listitem.name 移动到连接子句中,然后再次使用不同的条件连接到同一个表。然后我们根据我们使用的条件(大小、颜色)为表格设置别名。这样您就可以引用别名并提取正确的项目 ID,然后一次将它们连接起来。