我正在尝试使用 sql 模块在 go 中执行查询。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)但是我得到了错误pq: syntax error at or near "$1"如果我直接输入纪元值,那么查询将起作用,并且当我在没有任何变量的情况下尝试查询时,查询将起作用,即使用硬编码的查询。那么问题是什么?
1 回答
九州编程
TA贡献1785条经验 获得超4个赞
你是对的$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)
- 1 回答
- 0 关注
- 456 浏览
添加回答
举报
0/150
提交
取消