A
A
Andrey2017-02-11 18:40:02
Yii
Andrey, 2017-02-11 18:40:02

How to get the results of the selection in the order in which the conditions are given?

The select query has a condition

...
->andWhere(['in', 'product.id' , [2,21,3,64,38,35]])
->all();

What ->orderBy() should be so that the records are returned exactly in the order 2,21,3,64,38,35
If orderBy is not set, then it is sorted by id ASC.
--------
UPD: I was let down by the lack of Expression, this is how it works:
->orderBy([new \yii\db\Expression('FIELD (id, 2,21,3,64,38,35)')])

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
Papa, 2017-02-11
@reaferon

Depends on the database server.
For MySQL: Google ORDER BY FIELD()

D
Dmitry, 2017-02-11
@slo_nik

Good evening.
Is it necessary to sort by id? Maybe sorting on another field will suit you?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question