Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个名为 users 的 rails 表,并且很想知道从数据库中获取随机记录的最佳方法,因此它会像正常获取它一样快
@users = User.all
谢谢....
试试这个:
# Get 5 random users @users = User.order('RAND()').limit(5)
注意RAND()(MySQL) 函数RANDOM()在 PostgreSQL 和 SQLite 中被调用。
RAND()
RANDOM()