什么是将表数据与另一表数据进行比较的最佳方法?

时间:2011-11-09 16:46:11

标签: sql-server sql-server-2008 tsql

好的,我有2个选择语句:

SELECT DISTINCT Email
FROM         dbo.Customers
WHERE     (Email IS NOT NULL) 
AND (Email LIKE '%@%') 
AND (RIGHT(Email, 4) IN ('.net', '.com', '.org')) 
AND (Email NOT LIKE '%@UniformCity.com') 
AND (Email NOT LIKE '%@LifeUniform.com') 
AND (CHARINDEX('.',Email) <> 1)
AND (RIGHT(RTRIM(Email), 1) <> '.') AND (LEFT(LTRIM(Email), 1) <> '@') 
AND (Email not Like '%[`:;_*-,^[^]()+%\/=#-]%' escape '^') order by Email

AND

SELECT  replace(replace(
replace(
replace(EmailOptOut,'"','')
, ',held','')
, ',unsub','')
, ',confirm','') 
as CleanEmail
FROM [LifeMail].[dbo].[EmailOptOuts]

我只想从Customers表中选择电子邮件,而不是在EmailOptOuts表中。什么是实现这一目标的最佳方式?我想过使用游标和循环,但我相信你们专家知道更好的方法。

5 个答案:

答案 0 :(得分:3)

绕过清理规则只是为了让下面的代码更清晰,你可以这样做:

SELECT c.Email
    FROM dbo.Customers c
    WHERE NOT EXISTS(SELECT NULL
                         FROM [LifeMail].[dbo].[EmailOptOuts] o
                         WHERE c.Email = o.EmailOptOut)

答案 1 :(得分:2)

我将这些表放入临时表中,但如果需要,可以组合查询。

SELECT DISTINCT Email
INTO    #tCustomersEmail
FROM    dbo.Customers
WHERE   ( Email IS NOT NULL )
    AND ( Email LIKE '%@%' )
    AND ( RIGHT(Email, 4) IN ( '.net', '.com', '.org' ) )
    AND ( Email NOT LIKE '%@UniformCity.com' )
    AND ( Email NOT LIKE '%@LifeUniform.com' )
    AND ( CHARINDEX('.', Email) <> 1 )
    AND ( RIGHT(RTRIM(Email), 1) <> '.' )
    AND ( LEFT(LTRIM(Email), 1) <> '@' )
    AND ( Email NOT LIKE '%[`:;_*-,^[^]()+%\/=#-]%' ESCAPE '^' )
ORDER BY Email

SELECT  REPLACE(REPLACE(REPLACE(REPLACE(EmailOptOut, '"', ''), ',held', ''),',unsub', ''), ',confirm', '') AS CleanEmail
INTO #tOptOuts
FROM    [LifeMail].[dbo].[EmailOptOuts]

SELECT Email
FROM #tCustomersEmail AS tce
WHERE Email NOT IN (SELECT CleanEmail FROM #tOptOuts AS too)

答案 2 :(得分:1)

您还可以执行以下操作:

SELECT Email     
FROM dbo.Customers      
WHERE Email NOT IN 
    ( SELECT Email                          
      FROM [LifeMail].[dbo].[EmailOptOuts]                          
    ) 

答案 3 :(得分:1)

另外一个解决方案(使用LEFT JOIN):

SELECT Email     
FROM dbo.Customers  c
LEFT JOIN [LifeMail].[dbo].[EmailOptOuts] b ON (b.EmailOptOut = c.Email)
WHERE b.EmailOptOut IS NULL

答案 4 :(得分:1)

SELECT qCustomerEmail.Email 
FROM 
    (SELECT DISTINCT Email
    FROM         dbo.Customers
    WHERE     (Email IS NOT NULL) 
    AND (Email LIKE '%@%') 
    AND (RIGHT(Email, 4) IN ('.net', '.com', '.org')) 
    AND (Email NOT LIKE '%@UniformCity.com') 
    AND (Email NOT LIKE '%@LifeUniform.com') 
    AND (CHARINDEX('.',Email) <> 1)
    AND (RIGHT(RTRIM(Email), 1) <> '.') AND (LEFT(LTRIM(Email), 1) <> '@') 
    AND (Email not Like '%[`:;_*-,^[^]()+%\/=#-]%' escape '^')) qCustomerEmail
LEFT JOIN
    (SELECT  replace(replace(
    replace(
    replace(EmailOptOut,'"','')
    , ',held','')
    , ',unsub','')
    , ',confirm','') 
    as CleanEmail
    FROM [LifeMail].[dbo].[EmailOptOuts]) qOptOut
ON qCustomerEmail.Email = qOptOut.CleanEmail
WHERE qOptOut.CleanEmail IS NULL