随机查询排序记录
query sort records randomly
我在我的视图中显示了我的用户 table 的所有用户,在 table 中有一个名为 "order" 的字段,我在其中为前 200 个分配了一个数字记录,以便这些在 "order" 字段中具有值的用户将首先显示在该字段中具有空值的用户。
是否可以让前 200 名用户先随机显示?
这是我的查询:
@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?
ORDER BY CASE WHEN order <= 200
THEN RAND()
ELSE order
END
我会推荐:
order by (case when order is not null) desc,
rand()
我在我的视图中显示了我的用户 table 的所有用户,在 table 中有一个名为 "order" 的字段,我在其中为前 200 个分配了一个数字记录,以便这些在 "order" 字段中具有值的用户将首先显示在该字段中具有空值的用户。
是否可以让前 200 名用户先随机显示?
这是我的查询:
@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?
ORDER BY CASE WHEN order <= 200
THEN RAND()
ELSE order
END
我会推荐:
order by (case when order is not null) desc,
rand()