3

我在表 Product_Category (MSSQL 2008 r2) 中有简单的多对多关系:

CREATE TABLE #Product_Category (ProductId int, CategoryId int);
go
INSERT INTO #Product_Category (ProductId, CategoryId)
VALUES (1, 200);
go
INSERT INTO #Product_Category (ProductId, CategoryId)
VALUES (2, 200);
go
INSERT INTO #Product_Category (ProductId, CategoryId)
VALUES (2, 400);
go
INSERT INTO #Product_Category (ProductId, CategoryId)
VALUES (3, 300);
go
INSERT INTO #Product_Category (ProductId, CategoryId)
VALUES (2, 300);
go
DROP TABLE #Product_Category

如何选择具有以下条件的 ProductId:CategoryId = 200CategoryId = 300CategoryId = 400?

查询示例(下面的sql不起作用):

SELECT ProductId FROM #Product_Category
WHERE CategoryId = ALL (select 200 union select 300 union select 400)

我期望结果:ProductId = 2

4

7 回答 7

4
select PC.ProductId
from #Product_Category as PC
where PC.CategoryId in (200, 300, 400)
group by PC.ProductId
having count(distinct PC.CategoryId) = 3
于 2013-03-02T11:17:31.797 回答
1

这里我使用的是 CTE,但您可以使用表变量或其他用于category_filter.

with category_filter as (
    select * from (values (200), (300), (400)) as v(id)
)
select distinct ProductId
from #Product_Category
join category_filter
    on (#Product_Category.CategoryId = category_filter.id)
group by ProductId
having COUNT(distinct CategoryId) = (select COUNT(*) from category_filter)
于 2013-03-02T05:47:16.937 回答
1

更新:它仍然很丑,但它确实有效:

SELECT DISTINCT master.ProductId
FROM #Product_Category master
JOIN (
    SELECT ProductId, 
           cat200 = max(case when CategoryId=200 then 1 else 0 end),
           cat300 = max(case when CategoryId=300 then 1 else 0 end),
           cat400 = max(case when CategoryId=400 then 1 else 0 end)
   FROM #Product_Category
   GROUP BY ProductId
) sub ON sub.ProductId = master.ProductId
WHERE cat200=1
  and cat300=1
  AND cat400=1
于 2013-03-02T05:24:01.903 回答
1

尝试这个

SELECT a.ProductId 
    FROM Product_Category as a, 
        Product_Category as b, 
        Product_Category as c 
        WHERE a.CategoryId = 200 
            And b.`CategoryId` = 300 
            And c.`CategoryId` = 400 
            And a.`ProductId` = b.`ProductId` 
            And b.`ProductId` = c.`ProductId`

更多像 500 和 600

SELECT a.ProductId 
    FROM Product_Category as a, 
        Product_Category as b, 
        Product_Category as c,
        Product_Category as d,
        Product_Category as e,
        WHERE a.CategoryId = 200 
            And b.`CategoryId` = 300 
            And c.`CategoryId` = 400 
            And d.`CategoryId` = 500 
            And e.`CategoryId` = 600 
            And a.`ProductId` = b.`ProductId` 
            And b.`ProductId` = c.`ProductId`
            And c.`ProductId` = d.`ProductId`
            And d.`ProductId` = e.`ProductId`

检查现场演示http://sqlfiddle.com/#!2/8965e/1/0

于 2013-03-02T05:24:15.220 回答
0
WITH L AS (
SELECT *
  FROM (VALUES (200),(300),(400)) AS T(CategoryId)
)
SELECT ProductId
  FROM Product_Category P
 INNER JOIN L
    ON L.CategoryId = P.CategoryId
 GROUP BY ProductId
HAVING COUNT(1) = (SELECT Count(1) FROM L)
;

如果您打算使用 TVP,则 WITH 会消失。

于 2013-03-02T18:41:39.300 回答
0

您可以将值用作表源并在 WHERE 子句中使用 NOT EXISTS 和EXCEPT运算符检查它们

SELECT *
FROM #Product_Category p
WHERE NOT EXISTS (                                                        
                  SELECT Match
                  FROM (VALUES(200), 
                              (300),
                              (400))
                  x(Match)
                  EXCEPT
                  SELECT CategoryId
                  FROM #Product_Category p2
                  WHERE p.ProductID = p2.ProductID
                  )  

SQLFiddle上的演示

于 2013-03-02T11:18:41.717 回答
0

也是丑陋的解决方案:)

WITH category_filter1(CategoryId) AS (
    SELECT * FROM (VALUES (200), (300), (400)) tmp1(tmp2)
)
SELECT p.ProductId
FROM (
    SELECT ProductId,
        CASE WHEN CategoryId IN (SELECT CategoryId FROM category_filter1) THEN 1 ELSE 0 END f
    FROM #Product_Category
    ) p
GROUP BY p.ProductId, p.f
HAVING COUNT(*) = (SELECT COUNT(*) FROM category_filter1);
于 2013-03-02T06:43:50.550 回答