首页 文章

两个表加入时自我加入

提问于
浏览
0

我有2个表与父外键关系Table1 - (id1,pIntID,fid)id1-主键,fid外键Table2 - (fid,value)fid主键

值将类似于这个table1

id1, pIntID, fid
1    IR1     AC
2    IR1     RJ
3    IR2     AC
4    IR2     AC

表2

fid, value
AC   accept
RJ   reject

我必须从表一中选择那些tablle2.value = Accept但不是Reject的pIntID . 我可以使用子查询来做到这一点,但是这会对大量数据造成严重影响,是否有最佳方法可以做到这一点?

这是工作查询:

select t1.pIntid
from table1 t1 join
     table2 t2
     on t1.fid = t2.fid
where t2.value = 'accept' and
      t1.pIntid not in (select tx1.pintid
                        from table1 tx1 join
                             table2.tx2
                             on tx1.fid = tx2.fid
                        where tx2.value = 'reject'
                       )

2 回答

  • 0

    作为另一种选择,您可以使用反连接模式 .

    SELECT t1.pIntid
       FROM table1 t1
       JOIN table2 t2
         ON t2.fid   = t1.fid
        AND t2.value = 'accept'
       LEFT
       JOIN table2 tx2
         ON tx2.fid   = t1.fid
        AND tx2.value = 'reject'
      WHERE tx2.fid IS NULL
    

    反连接模式是外连接,WHERE子句中的条件排除了找到匹配项的行 .

    为了获得最佳性能,我们希望在table2上提供适当的覆盖索引 . 使用 fidvalue 作为索引中的前导列 .

    如果table2中有多行,对于同一个fid,value ='accept',则join操作将产生“重复”行 .

  • 0

    这可能有点扭曲,因为它在where子句中有约束,但我相信会起作用:

    select t1.pintid
    from table1 t1
    join table2 t2 on t1.fid = t2.fid
    where t2.value in ('accept', 'reject')
    group by t1.pintid
    having count(distinct t2.value) = 1
       and t2.value = 'accept'
    

    另一种解决方案(我猜是更干净)将使用 NOT EXISTS 来排除那些包含 accept 但也包含 reject 状态的 pintid

    select t1.pintid
    from table1 t1
    join table2 t2 on t1.fid = t2.fid
    where t2.value = 'accept'
      and not exists (
        select 1
        from table1 t1x
        join table2 t2x on t1x.fid = t2x.fid
        where t2x.value = 'reject'
          and t1.pintid = t1x.pintid
        )
    group by t1.pintid
    

相关问题