I
I
Igor Vasiliev2017-09-24 18:25:29
Yii
Igor Vasiliev, 2017-09-24 18:25:29

Sorting sql in strict order, how to set this query in Yii2?

Hello.
----
Perhaps someone faced such a problem
SELECT * FROM `category` ORDER BY `priority`;
Outputs:

1
10
100
111
2
22
202
3
333
4

But in order. Well, there is such a tricky way to sort everything in strict accordance:
SELECT * FROM `category` ORDER BY (priority+0);
the result is:
1
2
3
4
10
22
100
111
202
333

Excellent )))
Question:
How to do this in Yii2? If for example the Category class

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Igor Vasiliev, 2017-09-24
@Isolution666

Who had a similar problem, just make a query like this in the database:

Category::find()->orderBy('LENGTH(priority), priority')->all()

M
Maxim Kolokolnikov, 2017-09-24
@maxmirazh33

www.yiiframework.com/doc-2.0/yii-db-expression.html

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question