Hitu Bansal
Hitu Bansal

Reputation: 3137

MySql self Join with array in

I have a table named user, having these values:

 id    email 
  1    [email protected]
  2    [email protected]

Now, I need a query like :

SELECT id, email from user where email in ('[email protected]`,`[email protected]`,`[email protected]`)

As [email protected] doesn't exist in the table, I want the following results:

id    email 
1     [email protected]
2     [email protected]
NULL  [email protected]

Any idea how to do this in MySql?

Upvotes: 1

Views: 105

Answers (1)

Robby Cornelissen
Robby Cornelissen

Reputation: 97152

Here's one way:

SELECT user.id, user.email
FROM user
RIGHT JOIN (
    SELECT '[email protected]' AS email
        UNION SELECT '[email protected]'
        UNION SELECT '[email protected]'
    ) AS tmp
ON user.email = tmp.email;

Upvotes: 3

Related Questions