Reputation: 367
I have a problem with parameters to an SQL query. In this case I'm getting unexpected results from an ORDER BY query. I'm running PostgreSQL[1] on OSX and go v1.0.3.
I have a table with two columns: name varchar, gophers int
Inserted rows are: ("Alice", 2) and ("Bob", 1)
If I run my query like this: rows, err := db.Query("SELECT name FROM foo ORDER BY gophers")
I get what I want ("Bob", "Alice")
But if I run it like this rows, err = db.Query("SELECT name FROM foo ORDER BY $1", "gophers")
I get them by insertion order ("Alice", "Bob")
Why?
Complete code: http://paste2.org/p/2537881
[1] https://github.com/bmizerany/pq
Upvotes: 1
Views: 2579
Reputation: 5195
I don't know that this is a go issue at all. In psql I did
PREPARE test (text) AS SELECT name FROM foo ORDER BY $1;
EXECUTE test('a');
and I got
name
-------
alice
bob
(2 rows)
I am wondering if you can do ORDER BY $1. I Google this a bit but could not find anything usefully. I know this is not an answer but it would seem to me this might be the issue. I wish I could find something of if ORDER BY supports parameters.
Upvotes: 2