当任何外键2出现在第二个表中时,SQL查找外键为1的所有行

当任何外键2出现在第二个表中时,SQL查找外键为1的所有行,sql,sql-server,Sql,Sql Server,我的查询写作技能已经达到极限。 我有下表,其中extid+extdt的组合是一种复合键: ents entid | extid | extdt | itemid | ======================================= 1000 | 100 | '2016-08-01' | 1 | 1001 | 100 | '2016-08-01' | 2 | 1002 | 200 | '2016-08-01' | 3

我的查询写作技能已经达到极限。 我有下表,其中extid+extdt的组合是一种复合键:

 ents
 entid | extid |    extdt     | itemid |
 =======================================
  1000 |  100  | '2016-08-01' |   1    |
  1001 |  100  | '2016-08-01' |   2    |
  1002 |  200  | '2016-08-01' |   3    |
  1003 |  100  | '2016-08-02' |   4    |
  1004 |  200  | '2016-08-02' |   5    |
  1005 |  100  | '2016-08-02' |   6    |
因此,如果itemid(1或2)在items表中,查询将返回第1000行和第1001行。如果itemid3存在,则返回第1002行,依此类推

items
    itemid | itemDesc | 
    ===================  
       1   |   'fu'   | 
       3   |   'bar'  |
       4   |  'blah'  |
有了上面的项目表,我希望能够返回:

 entid | extid |    extdt     | itemid |
 =======================================
  1000 |  100  | '2016-08-01' |   1    |
  1001 |  100  | '2016-08-01' |   2    |
  1002 |  200  | '2016-08-01' |   3    |
  1003 |  100  | '2016-08-02' |   4    |
  1005 |  100  | '2016-08-02' |   6    |
我想不出一个聚合函数可以实现我所寻找的功能,它似乎也不存在。我挂断了对项目ID的分组。。。谁能给我指一下正确的方向吗

select * 
from ents e1 
where e1.extid in
    (select extid 
     from ents e2 
     where e2.itemid in (select itemid from items))
也许吧?您还可以修改您特定需要的项目ID的最后一个内部查询。

来自您的描述(但不是与之相反的示例):


但我怀疑问题并不是那么简单?

只要根据逻辑将它们连接起来就行了

SELECT e.*
-- records from the ents table
FROM ents e
-- with an extid that matches
JOIN ents extid on e.extid = extid.extid
-- all the records with an itemid in the items table.
JOIN items i on extid.itemid = i.itemid
如果唯一键为id和日期,则使用

JOIN ents extid on e.extid = extid.extid and e.extdt = extid.extdt

首先,您需要获得与项目匹配的组合键,但要包括
DISTINCT
,以避免重复

SELECT  DISTINCT extid,  extdt
FROM ents
JOIN items
  ON ents.itemid = items.itemid 
现在,您将检索与所选组合键匹配的每一行

SELECT *
FROM ents
JOIN ( SELECT  DISTINCT extid,  extdt
       FROM ents
       JOIN items
         ON ents.itemid = items.itemid 
     ) comp_key
 ON ents.extid = comp_key.extid
AND ents.extdt = comp_key.extdt

@techspider不仅仅是
内部连接
输出的逻辑是什么?即使items表中不存在项2、6行,您也要显示它们吗?您的预期结果毫无意义--为什么包含项ID为2和6的行?@Hogan我需要查看项2和6,因为它们与项1和4共享extid和extdt。。。有人把一个业务流程塞进了一张桌子,而这张桌子不是用来容纳它的。遗憾的是,我无法解决这个问题。@NoTheOtherFry那么逻辑是items表中具有itemid的项的extid的项?我认为他想要的是,对于他选择的项ID,他想要显示具有匹配extid的所有行。因此,一种可能的方法是两次加入ents表(我在上面发布了代码),这非常有助于让我以稍微不同的方式看待这个问题。非常感谢。为了推广explict
JOIN
sintaxis的使用,Aaron Bertrand写了一篇关于它的好文章。这就成功了!(但我没有看到答案,直到我与其他建议一起研究并开始发布我自己的建议)首先获取所有在items中包含itemid的extid和extdt组合。然后返回并连接extid和extdt上的ents。完美的非常感谢。这对于将问题视为比我之前想象的更小的块非常有用。多谢各位
SELECT *
FROM ents
JOIN ( SELECT  DISTINCT extid,  extdt
       FROM ents
       JOIN items
         ON ents.itemid = items.itemid 
     ) comp_key
 ON ents.extid = comp_key.extid
AND ents.extdt = comp_key.extdt
SELECT e.* 
FROM [Test].[dbo].[ents] e,[Test].[dbo].[items] i
WHERE e.extid in (SELECT extid from [Test].[dbo].[ents] oe where oe.itemid=i.itemid)
and e.extdt in (SELECT extdt from [Test].[dbo].[ents] oe where oe.itemid=i.itemid)
order by itemid