如果list是另一个列表的子集,则在sql中选择行

时间:2016-04-22 11:02:57

标签: sql sql-server stored-procedures subset

我在Books-CommunityBooks-User之间有两个多对多映射表。 当我将UserId传递给存储过程时,如何让用户书中存在所有书籍的所有社区?

用户图书表

[UserId]----------------------------[BookId]

fd95eff5-394e-4d51-b1af-46723268485f----27

fd95eff5-394e-4d51-b1af-46723268485f----28

fd95eff5-394e-4d51-b1af-46723268485f----35

fd95eff5-394e-4d51-b1af-46723268485f----36

fd95eff5-394e-4d51-b1af-46723268485f----40

fd95eff5-394e-4d51-b1af-46723268485f----41

fd95eff5-394e-4d51-b1af-46723268485f----56

fd95eff5-394e-4d51-b1af-46723268485f----67

fd95eff5-394e-4d51-b1af-46723268485f----88

fd95eff5-394e-4d51-b1af-46723268485f----103

fd95eff5-394e-4d51-b1af-46723268485f----104

fd95eff5-394e-4d51-b1af-46723268485f----155

fd95eff5-394e-4d51-b1af-46723268485f----156

BooksCommunityMap

[BookId]-----[CommunityId]

29-----------23

30-----------23

32-----------23

34-----------23

35-----------23

36-----------23

我想选择用户图书中存在所有图书的所有社区

2 个答案:

答案 0 :(得分:2)

;WITH cte as
(
     SELECT UserID, CASE WHEN BookId IN (SELECT Id From @incomingTableValuedVariable) THEN 0 ELSE 1 END as NoBook
     FROM MAPTABLE 
),
cte2 as
(
     SELECT UserID, MAX(NOBook) as NoBook
     FROM cte 
     GROUP BY UserId
)
SELECT UserID
FROM cte2
WHERE NoBook = 0;

答案 1 :(得分:0)

在这里你可以了解using a Table-Valued Parameters。我给你一个查询的例子来得到你需要的东西:

DECLARE @tvv TABLE (id int)

INSERT INTO @tvv VALUES
(1),
(2)

;WITH users_books AS (
SELECT *
FROM (VALUES
(1,1),(2,1),(3,1),
(1,2),(2,2),(3,3),
(1,3),(2,3),(3,5),
(1,4),(2,4),(3,7),
      (2,5),(3,8),
      (2,6),(3,9)
) as t (UserID, BookID)
)

SELECT UserID
FROM users_books u
LEFT JOIN @tvv i 
    ON i.id = u.BookID
GROUP BY UserID
HAVING COUNT(id) = (SELECT COUNT(*) FROM @tvv)

输出:

UserID
-----------
1
2

(2 row(s) affected)