SQL Server T-SQL:如何在值列表中选择不在表中的值?

s3fp2yjn  于 2022-12-26  发布在  其他
关注(0)|答案(6)|浏览(194)

我有一个电子邮件地址列表,其中一些地址在表中,另一些不在表中。我想从该列表中选择所有电子邮件,无论它们是否在表中。
我可以获得邮件地址在表中的用户,如下所示:
第一个月
但是我如何在列表中选择表中不存在值呢?
而且,我怎么能这样选择:

E-Mail | Status
email1 | Exist  
email2 | Exist  
email3 | Not Exist  
email4 | Exist
e4yzc0pl

e4yzc0pl1#

适用于SQL Server 2008

SELECT email,
       CASE
         WHEN EXISTS(SELECT *
                     FROM   Users U
                     WHERE  E.email = U.email) THEN 'Exist'
         ELSE 'Not Exist'
       END AS [Status]
FROM   (VALUES('email1'),
              ('email2'),
              ('email3'),
              ('email4')) E(email)

对于以前的版本,您可以对派生表UNION ALL执行类似的操作-ing常量。

/*The SELECT list is the same as previously*/
FROM (
SELECT 'email1' UNION ALL
SELECT 'email2' UNION ALL
SELECT 'email3' UNION ALL
SELECT 'email4'
)  E(email)

或者,如果您只需要不存在的结果集(如标题所暗示的),而不是问题中给出的确切结果集,则可以简单地执行此操作

SELECT email
FROM   (VALUES('email1'),
              ('email2'),
              ('email3'),
              ('email4')) E(email)  
EXCEPT
SELECT email
FROM Users
xiozqbni

xiozqbni2#

您需要用这些值创建一个表,然后使用NOT IN。这可以通过临时表、CTE(公用表表达式)或Table Values Constructor(SQL-Server 2008中提供)来完成:

SELECT email
FROM
    ( VALUES 
        ('email1')
      , ('email2')
      , ('email3')
    ) AS Checking (email)
WHERE email NOT IN 
      ( SELECT email 
        FROM Users
      )

第二个结果可以通过LEFT JOINEXISTS子查询找到:

SELECT email
     , CASE WHEN EXISTS ( SELECT * 
                          FROM Users u
                          WHERE u.email = Checking.email
                        ) 
            THEN 'Exists'
            ELSE 'Not exists'
       END AS status 
FROM
    ( VALUES 
        ('email1')
      , ('email2')
      , ('email3')
    ) AS Checking (email)
hgc7kmma

hgc7kmma3#

你应该有一个包含要检查的电子邮件列表的表,然后执行以下查询:

SELECT E.Email, CASE WHEN U.Email IS NULL THEN 'Not Exists' ELSE 'Exists' END Status
FROM EmailsToCheck E
LEFT JOIN (SELECT DISTINCT Email FROM Users) U
ON E.Email = U.Email
shstlldc

shstlldc4#

当你不想有在数据库中的列表中的电子邮件,你可以做以下:

select    u.name
        , u.EMAIL
        , a.emailadres
        , case when a.emailadres is null then 'Not exists'
               else 'Exists'
          end as 'Existence'
from      users u
          left join (          select 'email1' as emailadres
                     union all select 'email2'
                     union all select 'email3') a
            on  a.emailadres = u.EMAIL)

这样你会得到这样的结果

name | email  | emailadres | existence
-----|--------|------------|----------
NULL | NULL   | a@b.com    | Not exists
Jan  | j@j.nl | j@j.nl     | Exists

在本例中,使用IN或EXISTS运算符比使用左连接更繁重。
祝你好运:)

ecbunoof

ecbunoof5#

这应该适用于所有SQL版本。

SELECT  E.AccessCode ,
        CASE WHEN C.AccessCode IS NOT NULL THEN 'Exist'
             ELSE 'Not Exist'
        END AS [Status]
FROM    ( SELECT    '60552' AS AccessCode
          UNION ALL
          SELECT    '80630'
          UNION ALL
          SELECT    '1611'
          UNION ALL
          SELECT    '0000'
        ) AS E
        LEFT OUTER JOIN dbo.Credentials C ON E.AccessCode = c.AccessCode
xtfmy6hx

xtfmy6hx6#

使用此版本:-- SQL Server 2008或更高版本

SELECT U.* 
FROM USERS AS U
Inner Join (
  SELECT   
    EMail, [Status]
  FROM
    (
      Values
        ('email1', 'Exist'),
        ('email2', 'Exist'),
        ('email3', 'Not Exist'),
        ('email4', 'Exist')
    )AS TempTableName (EMail, [Status])
  Where TempTableName.EMail IN ('email1','email2','email3')
) As TMP ON U.EMail = TMP.EMail

相关问题