qry := `
SELECT id, uuid, name
FROM users
ORDER BY ?
LIMIT ?
OFFSET ?
`
limit := getLimit() // 10 (int)
offset := getOffset() // 0 (int)
order := getOrder() // id DESC (string)
rows, err := db.QueryContext(ctx, qry, order, limit, offset)
When I run code above, ORDER BY
clause won’t affect the result because id DESC
is being wrapped within single quotes as shown below in the DB logs. Is there a solution to this (excluding fmt.Sprintf
)? Single quote is the issue here.
SELECT id, uuid, name
FROM users
ORDER BY 'id DESC'
LIMIT 10
OFFSET 0