IvanCloud IvanCloud - 1 year ago 55
MySQL Question

How can i perform order by where id =5 desc in yii 2

Customer::find()->where(['<>','customer_id', '7'])->all()

Currently the code above allows me to find all the customer where their customer id is not 7. Now, I want to find all

where customer_id is not 7 order by customer id = 5 desc

I know that I can do that in mysql using:

SELECT * FROM mydatabase.customer_id where customer_id <> 7 order by customer_id = 5 desc;

but how can I do that with

Answer Source

You can also use active record :

Customer::find()->where(['<>','customer_id', '7'])
->orderBy([new \yii\db\Expression('FIELD (customer_id, 5) DESC')])->all();