0

I am wanting to query a database and pull retrieve information from a number of rows but i am unsure of the MySQL Syntax. Something like this;

SELECT LastName FROM Users WHERE ID = 1 AND ID = 2 AND ID = 3

So i am wanting to receive back

Smith, Doe, and Doe

Users
ID    FirstName    LastName    
1     John         Smith
2     Joe          Doe
3     Jane         Doe
More data etc etc
0

1 Answer 1

6

A cleaner approach:

SELECT LastName FROM Users WHERE ID IN(1, 2, 3);

This is basically the same thing as writing:

SELECT LastName FROM Users WHERE ID = 1 OR ID = 2 OR ID = 3;

Your query will not return any rows, simply because you're using:

ID = 1 AND ID = 2 AND ID = 3

A table row can not have an ID of 1 AND an ID of 2. Hence the reason we use OR.

Sign up to request clarification or add additional context in comments.

Comments

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.