Dynamic ORDER BY sorting #1047
Answered
by
porsager
CzechCoder
asked this question in
Q&A
-
When I try to use a variable for ASC or DESC inside the ternary, I'm getting a syntax error. Is there a way to have dynamic sorting? I have a simple table with invoices and I want to be able to sort them asc or desc based on a variable generated by the table, but when I send it to the sql syntax, it doesn't want to make it work. Is there a way around this? |
Beta Was this translation helpful? Give feedback.
Answered by
porsager
Mar 20, 2025
Replies: 1 comment 1 reply
-
Yeah those are PostgreSQL keywords, so can't be passed as parameter. you should do something like: await sql`
select * from something
order by some_column ${ asc ? sql`asc` : sql`desc` }
` |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
porsager
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yeah those are PostgreSQL keywords, so can't be passed as parameter. you should do something like: