We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
Hello,
what is the best way to sort results by column values.
MySQL example: SELECT * FROM table ORDER BY FIELD(ID,1,5,4,3)
The text was updated successfully, but these errors were encountered:
I don't think there is a built-in function for this (could be wrong though), but you can use custom ordering like this:
query := client.MyModel.Query() query.Order(func (s *sql.Selector) { s.OrderExpr(sql.ExprFunc(func(b *sql.Builder) { b.WriteString("FIELD(") b.Ident(mymodel.FieldID) b.WriteString(", 1, 5, 4, 3)") })) })
or if those values come from user input:
query := client.MyModel.Query() query.Order(func (s *sql.Selector) { s.OrderExpr(sql.ExprFunc(func(b *sql.Builder) { b.WriteString("FIELD(") b.Ident(mymodel.FieldID) for _, value := range values { b.WriteString(", ") b.Arg(value) } b.WriteString(")") })) })
Sorry, something went wrong.
No branches or pull requests
Hello,
what is the best way to sort results by column values.
MySQL example:
SELECT * FROM table ORDER BY FIELD(ID,1,5,4,3)
The text was updated successfully, but these errors were encountered: