Hi,
I have one tables ids in an array and they are ordered in the way I want and I have to select data from another table using those ids and in a order they are listen in the array.
Pretty confusing but I was thinking of two solutions giving ORDER BY parameter the array but I do not know if that possible and another is to get all the necessary data and then turn it to array (mysql_fetch_assoc) then compare those two and somehow order the new array using the ids array. But I also do not know how to do this...
Any ideas?
views:
32answers:
2
A:
I am not sure if I got exactly what you're asking... probably you want to use the ORDER BY FIELD
clause.
e.g.:
SELECT something FROM tablename ORDER BY FIELD(id,5,6,3,2,4,1)
nico
2010-05-16 12:23:41
A:
If the 'order array' is in memory, it might make the most sense to query the target table into another array, and then use your 'order array' to traverse that array.
That said, keeping an sorted array of ID's around in memory has problems... how do you know that the sort order is still valid? For that matter, how do you know that the ID's are still valid. It's generally better to do this kind of thing directly in the SQL and let the database handle the details.
mschaef
2010-06-23 15:13:54