简体   繁体   English

连接两个表,其中第一个表的所有子记录与第二个表的所有子记录匹配

[英]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.

相关问题 选择第一个表中与第二个表中的每个记录匹配的所有记录 - Select all the records in the first table that match each of the records in the second MySQL 5.1.7有条件地联接两个表,从第二个表中按日期选择顶部记录,从第一个表中选择所有记录 - MySQL 5.1.7 conditional join of two tables selecting top record by date from second table and all records from first 两个表匹配JOIN条件。 第一张表没有记录 - Two tables match on JOIN condition. First table has no records 如何联接两个显示所有记录的表,其中表A不在表B中 - How to Join two tables showing all records Where Table A is Not In Table B 如何查询两个表并从第一个表返回所有记录,而不管第二个表中是否存在 - How to query two tables and return all records from first, regardless on whether exist in the second table 从两个表中获取所有记录,但仅从第二个表中获取不在第一个表中的记录 - get all records from two tables but from second table only the ones that are not in first mysql查询加入,比较两个表并返回第一个表中的所有记录 - mysql query to join, compare two tables and return all records in first table 第一个表中的所有记录和第二个表中的额外记录 - All records from first table and extra records from second table sql:联接两个表并显示记录,即使它们在第一个表中也不在第二个表中 - sql: join two tables and display records even if they are in first table and not in the second table MYSQL:两个表之间的UNION结果,如果在第二个表中找到PK,则从第一个表中删除记录 - MYSQL: UNION results between two tables where omitting records from first table if PK found in second table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM