Jon Clagg
Jon Clagg

Reputation: 224

MYSQL PHP Compare array of IDs to table and return new array in Order

I have a JavaScript array of numbers which correlate to IDs in a MYSQL table
ex:

[5,7,11,100,201,55,67] // ids

I want to compare this array with the MYSQL table, order by Date (or other arbitrary column), and return the same IDs but in order by Date. ex:

TABLE: someTable
- id column
- Date column
- ect column

How do I return just the IDs in order as efficiently as possible?
And would this function lag if I tried to return a lot IDs in order, say over 20k?

Upvotes: 0

Views: 216

Answers (2)

Boundless
Boundless

Reputation: 2464

SELECT someTable.id FROM someTable WHERE someTable.id in (5,7,11,100,201,55,67) ORDER BY someTable.date 

You'll have to do some performance testing to see if it lags.

Upvotes: 2

Jim
Jim

Reputation: 22656

SELECT id FROM someTable WHERE id IN (yourIdList) ORDER BY date;

Note that it may be possible to "cheat" if the date is a creation date and the ids are autoincrement. You could sort by id and this should be the same as sorting by date.

Upvotes: 2

Related Questions