0

I have the following users table:

id, username, email
1,  abcdef,   abcdef@gmail.com
2,  mnopqr,   mnopqr@gmail.com

I would like to get all records in the table where username in ('abcdef', 'ghijkl', 'mnopqr', 'stuvwx') and return null rows if the username is not found. So in the following table I am expecting the following result (in the same order that the username appear in my IN clause:

id, username, email
1,  abcdef,   abcdef@gmail.com
null,  null,   null -- null values for username ghijkl
2,  mnopqr,   mnopqr@gmail.com
null,  null,   null -- null values for username stuvwx

How can I achieve that?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
user765368
  • 19,590
  • 27
  • 96
  • 167

1 Answers1

0
CREATE TEMPORARY TABLE usernames (username VARCHAR(10) PRIMARY KEY);

INSERT INTO usernames VALUES ('abcdef'), ('ghijkl'), ('mnopqr'), ('stuvwx');

SELECT tu.username, u.email
FROM usernames AS tu
LEFT OUTER JOIN users AS u USING (username);
Bill Karwin
  • 538,548
  • 86
  • 673
  • 828