简体   繁体   English

SQL –在一个表中查找所有不存在链接记录的记录

[英]SQL – Find all records in one table where linked records don’t exist

I have a slight twist on the other “in one table but not another” threads I've seen. 我看到的其他“在一个表中但不在另一个表”中的线程略有不同。

I have 3 tables: 我有3张桌子:

ProductFeed
    ProductFeedID INT
    ProductName NVARCHAR(30)

Product
    ProductID INT
    ProductFeedID INT
    StorefrontID INT

Storefront
    StorefrontID INT
    StorefrontName NVARCHAR(30)
  • The ProductFeed table has one unique product per record. ProductFeed表的每个记录有一个唯一的产品。
  • The Product table can have multiple of the same ProductFeedID, but can only have 0 or 1 unique ProductFeedID record per storefront. 产品表可以有多个相同的ProductFeedID,但每个店面只能有0或1个唯一的ProductFeedID记录。
  • The Storefront table has 1 record per storefront. 店面表每个店面有1条记录。

What I wish to do is a query showing all ProductFeed records, with the StorefrontID and StorefrontName, which DO NOT contain a Product record for that storefront. 我希望做的是一个查询,显示所有带有StorefrontID和StorefrontName的ProductFeed记录,其中不包含该店面的产品记录。 So something like this: 所以像这样:

ProductFeedID   ProductName    StorefrontID     StorefrontName
   123             iPod             1              MyStore1
   123             iPod             4              MyStore4
   234             TShirt           2              MyStore2
   234             TShirt           4              MyStore4
   345             Coffee Mug       5              MyStore5
   etc.

I'm using SQL Server 2012. Can someone help? 我正在使用SQL Server2012。有人可以帮忙吗?

If I understand correctly, you can approach this problem is by generating all possible combinations of product feeds and store fronts, then use left join . 如果我理解正确,那么可以通过生成产品Feed和商店前沿的所有可能组合来解决此问题,然后使用left join The combinations that fail to match are the ones you want: 失败的组合是您想要的组合:

select pf.*, sf.*
from productfeed pf cross join
     storefront sf left join
     product p
     on p.ProductFeedID = pf.ProductFeedID and
        p.StoreFrontId = pf.StoreFrontId
where p.ProductId is null

-- I think this is more understandable. -我认为这是可以理解的。

    select pf.*, sf.*
    from productfeed pf  
    cross join storefront sf 
    where not exists (select 1 
                      from product p
                      where p.ProductFeedID = pf.ProductFeedID
                      and p.StoreFrontID = sf.StoreFrontID)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从一个表中查找另一个表中不存在的记录 - Find records from one table which don't exist in another 从表A中找到缺失记录表B中表C中不存在它们 - Find Missing Records From Table A For Table B Where They Don't Exist in Table C Select 来自一个表的记录,这些记录在另一个表中不存在,同时保留一个表中的记录,其中有多个 - Select records from a table, which don't exist in another table whilst keep records from one table where there are multiple 从不存在记录的另一个表插入表 - Insert into table from another table where the records don't exist 从一个表中查找不存在于另一个表中的记录,以及从第三个表映射的用户 - Find records from one table which don't exist in another & users mapped from third table MYSQL-从一个表中查找另一个表中不存在的记录 - MYSQL - Find records from one table which don't exist in another SQL 从一个表中查找所有记录,但仅从链接表中查找最近的记录 - SQL find all records from one table, but only the most recent record from a linked table 从中间表中获取记录,或者如果不存在则获取所有记录 - Fetch records from intermediate table or fetch all if they don't exist SELECT 匹配一个值但不与另一个值存在的所有记录 - SELECT all records that match one value but don't exist with another SQL查询:选择表1中存在于表2中的所有记录+表2中存在于表1中不存在的所有记录 - SQL Query: select all the records from table1 which exist in table2 + all the records from table2 which don't exist in table1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM