T-SQL: How to Select Values in Value List that are NOT IN the Table?

前端 未结 6 1998
天命终不由人
天命终不由人 2020-12-07 20:01

I have a list of e-mail addresses, some of them are in my table, some of them are not. I want to select all e-mails from that list and whether they are in the table or not.

6条回答
  •  长情又很酷
    2020-12-07 20:32

    You should have a table with the list of emails to check. Then do this query:

    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
    

提交回复
热议问题