Rand() In Yii2


can u guys help me again please

select distinct listing_id from ms_listing_stadt order by ontop desc, rand()

$query = Listingstadt::find()->select(['listing_id'],'distinct');


        'ontop' => SORT_DESC,

        'rand()' => SORT_DESC,


i am trying to pull a list from table, that list has special listings they are marked in ontop with “1” so they appears top but i want that the list never look like same so i want get little random from the list…(a bit randomized ontop )… i hope someone can understand what i am trying :blink:

but i know using rand() is very bad for performance. has yii2 any other solution ?

how can i realize it without using random ??


how many records do you have RAND() is the easiest way I can think of. if you dealing with small numbers then it should not be a problem. if you have large data set well in that case it could be problem or you can write the logic in PHP

I did a Classified app about 100,000 records every month use RAND() to list top listings on homepage and performance is pretty good

thank you

maybe can u tell me how to do subquery ?

select distinct listing_id,(Select angebotsschwerpunkte from ms_listing where id = listing_id) angebotsschwerpunkte from ms_listing_stadt order by ontop desc, rand()