D
D
Dmitry Petrik2015-04-29 08:29:15
Yii
Dmitry Petrik, 2015-04-29 08:29:15

Yii2. How to shuffle the results of a selection from a database?

Looking for the last 2 books added.

Books::find()->orderBy('creation_time')->limit(2)->all();

But I need these 2 books to be displayed in random order.
I tried doing this, but it doesn't work either:
Books::find()->orderBy('creation_time, RAND()')->limit(2)->all();

Captures the extra 3rd book.
Help advice. You only need the last 2 books. How to be?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrey Mokhov, 2015-04-29
@mokhovcom

Books::find()->orderBy('RAND()')->limit(2)->all();

L
Leonid Sysoletin, 2015-04-29
@sysoletin

Take two and then shuffle the array in PHP.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question