alloy
alloy

Reputation: 766

Get Matching MySQL Rows from List?

I've got a list of ID numbers, (ex: 100230, 123890, 342098...). I've also got a table, with one column devoted to ID numbers:

thisID | name | dateBirth | State
----------------------------------
192465 | Fred | 94-12-06  |  OR
197586 | Alex | 78-04-26  |  NM
197586 | Alex | 78-04-26  |  CA
178546 | Sam  | 65-12-01  |  NY
112354 | Katy | 89-06-22  |  CO
...

I need to return any rows with 'thisID' that matches any of the items in the list I've got. Also, note that sometimes there may be multiple rows with the same ID that match an item in the list... in that case, all matching records should be returned.

I've looked around, and seen some recommendations to use arrays or temporary tables or something, but nothing definitive. How should I do this?

Upvotes: 0

Views: 60

Answers (2)

Prerak Sola
Prerak Sola

Reputation: 10009

You can do like this:

select * from [table_name] where thisID in ([your IDs]);

It will return all the rows that match the given IDs.

See the SQLFiddle Demo

Upvotes: 1

RiggsFolly
RiggsFolly

Reputation: 94662

You can use the IN sql syntax for this, if I understand you correctly.

SELECT * FROM tablename 
WHERE thisID IN (100230, 123890, 342098);

Upvotes: 3

Related Questions