A
A
Alexey2016-09-14 14:06:49
MySQL
Alexey, 2016-09-14 14:06:49

How to get a response to a query to the database without sorting?

I make a request to the database to select products by id, and id follow in a certain order. For example, the query part FROM oc_product p WHERE p.product_id IN ('574','572','573'). In response, I get a sorted array. I need to get an array with the same id order as in the request. Processing on php of the received array has already been implemented. Interested whether it is possible to save the order of the id query sql.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Rsa97, 2016-09-14
@alexeyfedotof

ORDER BY FIELD(`p`.`product_id`, 574, 572, 573)
But, as with any use of the function, indexes will not work.
And yet, if in IN() or FIELD() all arguments are numeric, then there is a numerical comparison, if at least one argument is string, then all comparisons will be string, which works slower.

S
Smithson, 2016-09-14
@Smithson

If the sort order is not explicitly specified in the sql query, then the return order of the values ​​is not defined .
What you have there in the database follows in a certain order - the piano does not play. The server will return the way it is more convenient for him at this moment.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question