John Mack
John Mack

Reputation: 119

Correct MySQL Syntax for returning multiple rows

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

Upvotes: 0

Views: 65

Answers (1)

user399666
user399666

Reputation: 19879

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.

Upvotes: 6

Related Questions