SQL;如何仅将Where Condition用于一列

时间:2011-08-23 10:09:59

标签: sql sql-server database sql-server-2008

首先,

Select Count(*)
From PROSPECT_MASTER //Result:12730 row


Select Count(*)
From YAZ..MARDATA.S_MUSTERI // Result: 1112 row

这是我的疑问;

SELECT a.ID, a.TURQ_ID, a.UNVAN, a.TIP, a.AKTOR, 
       a.GSM_ALAN, a.GSM_TEL, a.IS_ALAN, a.IS_TEL, 
       a.IS_EXT, a.EV_ALAN, a.EV_TEL, a.ADRES, 
       a.STF_KONTAK, 
       (CASE WHEN A.YAZ_ADRES IS NULL
         THEN (B.IS_ADRES1 +' '+B.IS_ADRES2) 
         ELSE A.YAZ_ADRES END) 
       COLLATE DATABASE_DEFAULT AS YAZ_ADRES , A.SEHIR, A.PS, A.MEMO
FROM PROSPECT_MASTER A, YAZ..MARDATA.S_MUSTERI B
WHERE A.ID IN (B.TC_KIM_NO, B.VERGI_NO) // Result 1070 row

正常情况下,A.ID IN (B.TC_KIM_NO, B.VERGI_NO)条件正在处理我的完整查询,但我希望此条件仅适用于一列(YAZ_ADRES),因为 A.ID 并非全部< strong> B.TC_KIM_NO 或全部 B.VERGI_NO

我的查询应获得12730行,如PROSPECT_MASTER表的计数。但我想仅为A.ID IN (B.TC_KIM_NO, B.VERGI_NO)列运行YAZ_ADRES条件。

我该怎么做?

1 个答案:

答案 0 :(得分:2)

听起来我觉得你需要的是一个外连接?

SELECT a.ID, a.TURQ_ID, a.UNVAN, a.TIP, a.AKTOR, 
       a.GSM_ALAN, a.GSM_TEL, a.IS_ALAN, a.IS_TEL, 
       a.IS_EXT, a.EV_ALAN, a.EV_TEL, a.ADRES, 
       a.STF_KONTAK, 
       (CASE WHEN A.YAZ_ADRES IS NULL
         THEN (B.IS_ADRES1 +' '+B.IS_ADRES2) 
         ELSE A.YAZ_ADRES END) 
       COLLATE DATABASE_DEFAULT AS YAZ_ADRES , A.SEHIR, A.PS, A.MEMO
FROM PROSPECT_MASTER A
LEFT OUTER JOIN YAZ..MARDATA.S_MUSTERI B
ON A.ID IN (B.TC_KIM_NO, B.VERGI_NO) // Result 12730 rows