如何在sql server中通过查询检查是否存在

时间:2014-11-06 02:43:22

标签: sql-server

我有两个查询,我想检查查询A返回的PopulateKode中是否存在查询A中的PopulateKode之一。

我该怎么做?

这是我的疑问:

QUERY A:

 select KodePosition as PopulateKode from Position where UserLogin='winz\siti.azzahra'

A的结果集:

PopulateKode
0041
0139

QUERY B:

 SELECT (A.IdPositiionPengirim +';'+ B.KodeJabatan)as PopulateKode
 FROM SuratKeluar as A
 join SuratKeluarD as B on A.Id=B.IdSuratKeluar
 where A.Id='60083a60-2643-45b5-8b59-c6094ec8b79d'

B的结果集

PopulateKode 
0037
0038

1 个答案:

答案 0 :(得分:0)

我会将IN()UNION一起使用,因此查询B中的每个值都是独立的:

select KodePosition as PopulateKode
from Position
where UserLogin='winz\siti.azzahra'
and KodePosition IN (
    SELECT A.IdPositiionPengirim
    FROM SuratKeluar as A
    join SuratKeluarD as B on A.Id=B.IdSuratKeluar
    where A.Id='60083a60-2643-45b5-8b59-c6094ec8b79d'
    UNION
    SELECT B.KodeJabatan
    FROM SuratKeluar as A
    join SuratKeluarD as B on A.Id=B.IdSuratKeluar
    where A.Id='60083a60-2643-45b5-8b59-c6094ec8b79d')