Order Records Using IN


I did this last year but I can’t remember how…

Im using WHERE IN to select my records, however I want to order the records by the order in which they were selected.

What I mean is, i select 1,3,4,2 and I want it to return the records as 1,3,4,2 however at the moment when I loop and render to the page i’m essentially getting 1,4,2,3.

There is a way to do this isn’t here any help would be greatly appreciated.


i trust you have also fixed your strings, i.e. change this –

WHERE `category_handle` IN ('dvd,tv,sci-fi')

to this –

WHERE `category_handle` IN ('dvd','tv','sci-fi')


ORDER BY field(id, 1, 3, 4, 2)

Ah i’ve sorted it. In your example where you put field i thought that was where i put my field name.

Thanks for the help.
Much appreciated.

I get an error using that method: FUNCTION category_handle does not exist.

This is the query:

	FROM `categories`
	WHERE `category_handle` IN ('dvd,tv,sci-fi')
	ORDER BY category_handle('dvd,tv,sci-fi')

Any Ideas?

In hindsight I could have made it less confusing by capitalising the function name!