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

order by rand()? #811

Open
Carseason opened this issue Mar 26, 2023 · 3 comments
Open

order by rand()? #811

Carseason opened this issue Mar 26, 2023 · 3 comments
Assignees

Comments

@Carseason
Copy link

Carseason commented Mar 26, 2023

how order by rand?

miseyu added a commit to miseyu/gen that referenced this issue Jun 29, 2023
miseyu added a commit to miseyu/gen that referenced this issue Jun 29, 2023
jinzhu pushed a commit that referenced this issue Aug 2, 2023
@randree
Copy link

randree commented Oct 5, 2023

Can you provide an example how to use this? Also, is there a way of using "RANDOM()" in case of Postgres database?

jeepc pushed a commit to jeepc/gen that referenced this issue Nov 20, 2023
@DarkAngel7
Copy link

DarkAngel7 commented Aug 19, 2024

Can you provide an example how to use this? Also, is there a way of using "RANDOM()" in case of Postgres database?

Here is some sample code to select random rows from table.

err = unusedWordsQuery.
     Order(field.Func.Rand()).
     Limit(num).
     Scan(&wordIds)

@randree
Copy link

randree commented Aug 20, 2024

Thanks for the answer!
Unfortunately, I'm working with Postgres and there is no function RAND() only RANDOM().
I found a solution but its not very beautiful.

... := q.ReplaceDB(q.Product.UnderlyingDB().Clauses(clause.OrderBy{
		Expression: clause.Expr{SQL: "RANDOM()"},
	})).Product.Find()

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

4 participants