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 not working in prepared stataments #274

Closed
labkode opened this issue Aug 30, 2014 · 2 comments
Closed

ORDER BY not working in prepared stataments #274

labkode opened this issue Aug 30, 2014 · 2 comments

Comments

@labkode
Copy link

labkode commented Aug 30, 2014

I tried to run this sql command, the query uses the limit and offser but bypass the orderBy.
In the other hand, If I wrote the query manually (SELECT * FROM partner ORDER BY id DESC) works perfectly

    // The inputs I recive are of this type orderBy string, limit, offset int64
    query := "SELECT * FROM partner ORDER BY ? LIMIT ? OFFSET ?"
    stmt, err := db.Prepare(query)
    if err != nil {
        return
    }
    defer stmt.Close()
    rows, err := stmt.Query(orderBy, limit, offset)
@xaprb
Copy link

xaprb commented Aug 31, 2014

This isn't related to this driver, Go, or database/sql. I suggest trying to run those queries from the terminal directly. As far as I recall, the LIMIT and OFFSET parameters cannot be parameters, they must be literals that are inserted directly into the query.

@julienschmidt
Copy link
Member

Within prepared statements, LIMIT parameters can be specified using ? placeholder markers.

http://dev.mysql.com/doc/refman/5.7/en/select.html
Since OFFSET is part of the LIMIT clause, the placeholder there is probably also fine.

But I think placeholders my not be used as an aggregate (GROUP BY) function parameter. I can not find a statement on this in the MySQL docs, but it would make a lot of sense when taking into account how aggregate functions are usually implemented.

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

3 participants