简体   繁体   English

查询随机排序记录

[英]query sort records randomly

I am showing in my view all the users of my user table, in the table there is a field called "order" in which I assign a number to the first 200 records so that these users who had the value in the "order" field are will show first that those who had null in that field.我在我的视图中显示了我的用户表的所有用户,在表中有一个名为“order”的字段,其中我为前 200 条记录分配了一个数字,以便这些在“order”字段中具有值的用户are 将首先显示那些在该字段中为空的人。

Is it possible that those first 200 users can show them first and randomly?前 200 个用户是否有可能首先随机显示它们?

this is my query:这是我的查询:

@users = User.includes(:plan).with_avatar



@users = @users.order( '`order` DESC, sponsor DESC, id DESC' ) 
@users = @users.paginate(page: page).per_page(18)

Is it possible that those first 200 users can show them first and randomly?前 200 个用户是否有可能首先随机显示它们?

ORDER BY CASE WHEN order <= 200
              THEN RAND()
              ELSE order
              END

I would recommend:我会推荐:

order by (case when order is not null) desc,
         rand()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM