This is my table:
Product
id | name
1 | A
2 | B
3 | C
4 | D
And I want ID 3 in first position:
Product
id | name
3 | C
1 | A
2 | B
4 | D
I can only with the "OrderBy" assign ASC and DESC values. It gives error if you assign a numeric value.
Use yii\db\Expression
:
$orderBy = (new \yii\db\Query())
->select('*')
->from('product')
->orderBy([new \yii\db\Expression('FIELD (id, 3,1,2,4)')])
->all();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With