Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternative SQL ways of looking up multiple items of known IDs?

Tags:

sql

Is there a better solution to the problem of looking up multiple known IDs in a table:

SELECT * FROM some_table WHERE id='1001' OR id='2002' OR id='3003' OR ...

I can have several hundreds of known items. Ideas?

like image 984
AlexanderJohannesen Avatar asked Apr 20 '09 14:04

AlexanderJohannesen


People also ask

What is the alternative for lookups for SQL?

virtualserver (id_virtualserver, ...)

How do I match multiple values in SQL?

Note – Use of IN for matching multiple values i.e. TOYOTA and HONDA in the same column i.e. COMPANY. Syntax: SELECT * FROM TABLE_NAME WHERE COLUMN_NAME IN (MATCHING_VALUE1,MATCHING_VALUE2);


1 Answers

SELECT * FROM some_table WHERE ID IN ('1001', '1002', '1003')

and if your known IDs are coming from another table

SELECT * FROM some_table WHERE ID IN (
    SELECT KnownID FROM some_other_table WHERE someCondition 
)
like image 117
Eoin Campbell Avatar answered Oct 09 '22 01:10

Eoin Campbell