[英]Join two tables where all child records of first table match all child records of second table
I have four tables: Customer, CustomerCategory, Limit, and LimitCategory. 我有四个表:Customer,CustomerCategory,Limit和LimitCategory。 A customer can be in multiple categories and a limit can also have multiple categories.
一个客户可以属于多个类别,一个限制也可以具有多个类别。 I need to write a query that will return the customer name and limit amount where ALL the customers categories match ALL the limit categories.
我需要编写一个查询,该查询将返回所有客户类别与所有限制类别匹配的客户名称和限制金额。
I'm guessing it would be similar to the answer here , but I can't seem to get it right. 我猜这将与此处的答案类似,但我似乎无法正确解决。 Thanks!
谢谢!
Edit - Here's what the tables look like: 编辑 -这是表格的样子:
tblCustomer
customerId
name
tblCustomerCategory
customerId
categoryId
tblLimit
limitId
limit
tblLimitCategory
limitId
categoryId
I THINK you're looking for: 我认为您在寻找:
SELECT *
FROM CustomerCategory
LEFT OUTER JOIN Customer
ON CustomerCategory.CustomerId = Customer.Id
INNER JOIN LimitCategory
ON CustomerCategory.CategoryId = LimitCategory.CategoryId
LEFT OUTER JOIN Limit
ON Limit.Id = LimitCategory.LimitId
Updated! 更新!
Thanks to Felix for pointing out a flaw in my existing solution (3 years after I originally posted it, hehe). 感谢Felix指出我现有解决方案中的一个缺陷(在我最初发布它的三年后,呵呵)。 After looking at it again, I think this might be correct.
再次查看之后,我认为这可能是正确的。 Here I'm getting (1) the customers and limits with matching categories, plus the number of matching categories, (2) the number of categories per customer, (3) the number of categories per limit, (4) I then ensure the number of categories for customer and limits is the same as the number of the matches between the customers and limits:
在这里,我得到(1)具有匹配类别的客户和限制,加上匹配类别的数量,(2)每个客户的类别数量,(3)每个限制的类别数量,(4)然后确保客户和限制的类别数与客户和限制之间的匹配数相同:
UNTESTED! 未测试!
select
matches.name,
matches.limit
from (
select
c.name,
c.customerId,
l.limit,
l.limitId,
count(*) over(partition by cc.customerId, lc.limitId) as matchCount
from tblCustomer c
join tblCustomerCategory cc on c.customerId = cc.customerId
join tblLimitCategory lc on cc.categoryId = lc.categoryId
join tblLimit l on lc.limitId = l.limitId
) as matches
join (
select
cc.customerId,
count(*) as categoryCount
from tblCustomerCategory cc
group by cc.customerId
) as customerCategories
on matches.customerId = customerCategories.customerId
join (
select
lc.limitId,
count(*) as categoryCount
from tblLimitCategory lc
group by lc.limitId
) as limitCategories
on matches.limitId = limitCategories.limitId
where matches.matchCount = customerCategories.categoryCount
and matches.matchCount = limitCategories.categoryCount
I don't know if this will work or not, just a thought i had and i can't test it, I'm sures theres a nicer way! 我不知道这是否行得通,只是我有一个想法,我无法测试它,我敢肯定有更好的方法! don't be too harsh :)
不要太苛刻:)
SELECT
c.customerId
, l.limitId
FROM
tblCustomer c
CROSS JOIN
tblLimit l
WHERE NOT EXISTS
(
SELECT
lc.limitId
FROM
tblLimitCategory lc
WHERE
lc.limitId = l.id
EXCEPT
SELECT
cc.categoryId
FROM
tblCustomerCategory cc
WHERE
cc.customerId = l.id
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.