1

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?

3
  • 1
    So where is your user_id and email_id? Commented Apr 16, 2015 at 3:28
  • @TimBiegeleisen: Could you please explain more Commented Apr 16, 2015 at 3:36
  • I know of no way a SQL statement can be injected when it isn't taking in user input. If those emails are taken from user input they should be separated out. I'd be interested to know how this could be injected as it currently stands, @TimBiegeleisen. Commented Apr 16, 2015 at 4:01

1 Answer 1

3

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;
Sign up to request clarification or add additional context in comments.

2 Comments

I m getting this error - Illegal mix of collations (latin1_swedish_ci,IMPLICIT) and (utf8mb4_general_ci,COERCIBLE) for operation '='

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.