SQL:如何使用Sub查询返回结果使用exists?

时间:2018-03-24 15:53:08

标签: sql sql-server subquery

我正在尝试使用EXISTS

从子查询中选择记录
select * from dbo.EMP
where exists (
select a.* from  dbo.HREMP a
inner join dbo.emp  b
on a.ID=b.ID
inner join dbo.LOGO c
on b.id=c.EMPID
inner join dbo.LOGOACC d
on c.BADGENO=d.BADGENO
where d.ACCLVID in (2191,2292,2293,2294,2295,2296,2297))
  

以上查询列出了EMP表的所有员工。?

我的要求是仅列出EMP表中存在于我的子查询中的员工。

3 个答案:

答案 0 :(得分:1)

您需要EXISTS中的相关性。

SELECT *
FROM   dbo.emp o
WHERE  EXISTS (SELECT 1
               FROM   dbo.hremp a
                      inner join dbo.emp b
                              ON a.id = b.id
                      inner join dbo.logo c
                              ON b.id = c.empid
                      inner join dbo.logoacc d
                              ON c.badgeno = d.badgeno
               WHERE  d.acclvid IN ( 2191, 2292, 2293, 2294,
                                     2295, 2296, 2297 )
                      AND a.id = o.id)  -- correlation between subquery and outer.

我认为这种关系对你有好处吗?

SELECT *
FROM   dbo.emp b
WHERE  EXISTS (SELECT 1
               FROM   dbo.hremp a
                      inner join dbo.logo c
                              ON a.id = c.empid
                      inner join dbo.logoacc d
                              ON c.badgeno = d.badgeno
               WHERE  d.acclvid IN ( 2191, 2292, 2293, 2294,
                                     2295, 2296, 2297 )
                      AND a.id = b.id) 

答案 1 :(得分:1)

EXISTS

  

指定要测试行是否存在的子查询。

在你的情况下dbo.emp已经位于FROM外部,因此您只需要在dbo.emp中将dbo.hremp ID与EXISTS ID相关联。

你可以试试这个。

SELECT * 
FROM   dbo.emp AS m
WHERE  EXISTS (SELECT 1 
               FROM   dbo.hremp a 
                      INNER JOIN dbo.logo c 
                              ON b.id = c.empid 
                      INNER JOIN dbo.logoacc d 
                              ON c.badgeno = d.badgeno 
               WHERE  d.acclvid IN ( 2191, 2292, 2293, 2294, 
                                     2295, 2296, 2297 ) AND m.id = a.id)

但我认为您可以直接使用它来获得您的期望。

SELECT b.* 
FROM   dbo.hremp a 
      INNER JOIN dbo.emp b 
              ON a.id = b.id 
      INNER JOIN dbo.logo c 
              ON b.id = c.empid 
      INNER JOIN dbo.logoacc d 
              ON c.badgeno = d.badgeno 
WHERE  d.acclvid IN ( 2191, 2292, 2293, 2294,2295, 2296, 2297)

修改

您可以尝试此查询。

SELECT b.* 
FROM   dbo.hremp a 
      INNER JOIN dbo.emp b 
              ON a.id = b.id 
      INNER JOIN dbo.logo c 
              ON b.id = c.empid 
      INNER JOIN dbo.logoacc d 
              ON c.badgeno = d.badgeno 
WHERE EXISTS 
(
    SELECT 1 
    FROM dbo.logoacc m
    WHERE 
        m.acclvid IN ( 2191, 2292, 2293, 2294,2295, 2296, 2297)
    AND 
        m.badgeno = d.badgeno 
)

EXISTS

答案 2 :(得分:0)

你没有将主要与次级联系连接。

select e.* 
from dbo.EMP e
where exists ( select 1 
                 from dbo.HREMP a 
                 join dbo.LOGO c
                   on c.EMPID = e.id 
                 join dbo.LOGOACC d
                   on d.BADGENO = c.BADGENO  
                  and d.ACCLVID in (2191,2292,2293,2294,2295,2296,2297)
                where a.ID = e.ID
             )

我敢打赌,这就是你要找的东西

select distinct e.* 
from dbo.EMP e
join dbo.HREMP a 
  on a.ID    = e.ID
join dbo.LOGO c
  on c.EMPID = e.id 
join dbo.LOGOACC d
  on d.BADGENO = c.BADGENO  
 and d.ACCLVID in (2191, 2292, 2293, 2294, 2295, 2296, 2297)