Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.
Sign upSupport for `order by random()` #1007
Comments
This comment has been minimized.
|
You can use |
sgrif
closed this
Jul 8, 2017
This comment has been minimized.
agersant
commented
Jul 8, 2017
|
Thanks! |
This comment has been minimized.
Thomasdezeeuw
commented
Oct 1, 2017
|
It took me too long to figure this out so this is the code required: no_arg_sql_function!(RANDOM, (), "Represents the sql RANDOM() function");
// Usage, using the post schema from the getting started guide.
let results = posts
.order(RANDOM)
.limit(5)
.load::<Post>(&*connection)
.expect("unable to load posts");Which will generate the following query: SELECT * ORDER BY RANDOM() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
agersant commentedJul 8, 2017
Setup
Versions
Feature Flags
Problem Description
There is no way to randomly sort the results of a select statement.
What are you trying to accomplish?
Returning a random subset of rows from a table. (ie.
SELECT * FROM my_table LIMIT 20 ORDER BY RANDOM())I know it's possible to work around this with
my_table.order(sql::<types::Bool>("RANDOM()")).load(connection)but this seems like a useful feature to support in a safe manner.Checklist