Orden de clasificación de MySQL por valor de matriz

I need to run a MySQL query where the order is determined by an array value.

My array is variable but the values in the array correspond to a field in my DB table called 'ID' so I want the result to be returned in the ID order 9, 1, 4.

Array ( [0] => 9 [1] => 1 [2] => 4 )

Is this possible in MySQL or would it be possible to sort the MySQL $result using the array after? You can assume the only values being returned are those in the array.

preguntado el 08 de noviembre de 11 a las 18:11

2 Respuestas

respondido 08 nov., 11:22

You want to get a list of items with the ids 5, 2, 1, 3 and output them in the same order. However, just running a select query will return the items in the order 1, 2, 3, 5.

To order the results correctly you need to build a list of ORDER BY items describing the weights of the ids. This will look like

ORDER BY id = 5 DESC, id = 2 DESC, id = 1 DESC, id = 3 DESC

Respondido 03 Feb 20, 05:02

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.