我正在尝试使用sql模块执行查询.
var from string = "2015-03-01 00:00:00"
rows, err := db.Query("select time, val from table where " +
"time >= extract(epoch from timestamp with time zone $1)::int4 " +
"and time < extract(epoch from timestamp with time zone '2015-03-01 00:15:10')::int4 " +
"order by time asc",from)
Run Code Online (Sandbox Code Playgroud)
但是我得到了错误
pq: syntax error at or near "$1"
Run Code Online (Sandbox Code Playgroud)
如果我直接输入epoch值,那么查询将起作用,并且当我在没有任何变量的情况下尝试它时查询工作,即查询硬编码.那么问题是什么?
小智 6
你说得对$1
和?
.
它抱怨语法无效的原因$1
是因为类型转换.这样说:
rows, err := db.Query("select time, val from table where " +
"time >= extract(epoch from $1::timestamp with time zone)::int4 " +
"and time < extract(epoch from timestamp with time zone '2015-03-01 00:15:10')::int4 " +
"order by time asc",from)
Run Code Online (Sandbox Code Playgroud)