I
I
idccc2014-10-31 20:50:09
PHP
idccc, 2014-10-31 20:50:09

How to sort in SQL query in order of array numbers?

Good afternoon.
There is json like {"1":"44","3":5,"4":1,"5":123}. For the task, we convert it to an array using json_decode.
It is required to make an SQL query of the form

SELECT *
FROM wp_posts
WHERE ID IN (массив)

so that the entries are sorted in the order in which the elements appear in the array. That is, for the selection to go in the following order:
id = 44, 5, 1, 123.
There is a complex solution in my head, I need a simple one.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
neolink, 2014-10-31
@idccc

SELECT *
FROM wp_posts
WHERE ID IN (1,5,4,3)
ORDER BY FIELD(ID,1,5,4,3)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question