Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Improvement] Add Ability to Order Query Results Randomly #275

Open
homestar9 opened this issue Jan 26, 2024 · 3 comments
Open

[Improvement] Add Ability to Order Query Results Randomly #275

homestar9 opened this issue Jan 26, 2024 · 3 comments

Comments

@homestar9
Copy link
Contributor

It would be valuable to have a method in QB to return a random row from the query. Perhaps we could have an orderByRandom() method.

Examples:

// return a random record from the product table
qb.from( 'product' ).orderByRandom().first();

// return 5 random records from the product table (note there currently isn't a `limit` option in QB
qb.from( 'product' ).orderByRandom().simplePaginate( 1, 5 );

Different implementations of random:
https://www.petefreitag.com/blog/sql-select-random-row/

@elpete
Copy link
Collaborator

elpete commented Jan 26, 2024

https://stackoverflow.com/questions/19412/how-to-request-a-random-row-in-sql

@homestar9
Copy link
Contributor Author

This might be something I could create a PR for. What do you think about the name orderByRandom()? Or do you think we should simply call it random()?

@elpete
Copy link
Collaborator

elpete commented Jan 26, 2024

orderByRandom is great!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants