1

我有这两个查询几乎给了我需要的数据:

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

我正在寻找的最终输出是 SizeItemID 与连接在 ItemID 上的 ColorItemID 连接。

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
4

3 回答 3

3

您可以将两个查询合并到 ItemId 上以构造最终值。

在以下查询中,我还在您的表名上使用了别名。许多人发现有意义的别名比长表名更容易阅读:

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
于 2012-08-31T13:18:41.337 回答
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

那么你就有了两个可以正常使用的简单视图,对吧?所以你的查询是:

从 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,然后一次将它们连接起来。

于 2012-08-31T13:38:44.970 回答
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
于 2012-08-31T13:08:59.617 回答