1

I am currently writing code that is supposed to query for several different records with multiple IDs. I have tried:

SELECT * FROM Items WHERE ID = ( 1, 2 )

SELECT * FROM Items WHERE ID = 1, 2

SELECT * FROM Items WHERE ID = '1, 2'

What am I doing wrong?

7 Answers 7

2

It would work just as well. What you're looking for information on is the "IN" operator of most SQL languages (MySQL example here).

what you're doing is providing a series of values as a set (right side of the IN operator) and then asking the database to bring back records whose column you're querying about (the left side of the IN operator) that have one of the values in the set.

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

Comments

2

use IN if you are comparing to multiple values.

SELECT * FROM Items WHERE ID IN (1, 2)

which is the same for

SELECT * FROM Items WHERE ID = 1 OR ID = 2

for this statement: "... query for several different records with multiple IDs", I think you want to search for a group which has multiple ID, if that's the case, the problem is called Relational Division.

SELECT groupColumn
FROM   Items 
WHERE  ID IN (1, 2)
GROUP  BY groupColumn
HAVING COUNT(*) = 2

Comments

2

What you need to type is

SELECT * FROM Items WHERE ID IN (1,2)

Note for string values inside the IN brackets use single quotes ('1','2')

Comments

2

Use IN like following

SELECT * FROM Items WHERE ID IN (1, 2);

Comments

2

SYNTAX:

SELECT * FROM TABLE WHERE ID IN (id1, id2, ..., idn)

Here in your case:

SELECT * FROM Items WHERE ID IN (1, 2);

Comments

2

Use this

SELECT * FROM Items WHERE ID = 1 or ID = 2;

or use short hand notation

SELECT * FROM Items WHERE ID IN (1,2);

Comments

2

use

select * from items where id in (1,2);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.