postgresql - 如何在 go 中使用 postgres 间隔?
问题描述
我有一个连接到 postgres 数据库的 go 项目。我想在一定天数内删除订单 - 我试过这样做:
type SQLOrderDatabase struct {
Connection *pgx.Conn
logger *logrus.Entry
}
interval := pgtype.Interval{}
_ = interval.Set(time.Second * 5)
_, err := database.Connection.Exec(ctx, "DELETE FROM store.items WHERE store.time > (now() AT TIME ZONE 'utc' - $1);", &interval)
这是使用 pgx 库和 pgx 类型。但是,它始终失败并出现此错误:
Error:ERROR: operator does not exist: timestamp without time zone \u003e interval (SQLSTATE 42883)","severity":"error","
反正有没有使用postgres间隔?
解决方案
There isn't enough context for postgres to know how you want to subtract the data passed as $1
so you need to explicitly tell it, which you can do by type casting the parameter.
(now() AT TIME ZONE 'utc' - $1::interval)
推荐阅读
- html - 为什么我不能在 .central1 div 中编辑 .above 类?
- java - n位的Java String HashCode?
- php - 如何删除 JSON 的某些部分
- django - Django + React 生产设置
- javascript - vue,使用javascript hook实现动画失败
- python - 使用 pylibdmtx 将 Datamatrix 解码为数字
- python - 按另一个数组的匹配值的顺序对 numpy 数组进行排序
- node.js - NodeJS:带有主机密钥的HTTPS服务器错误(错误的标签?)
- jquery - 将对象数组发送到 Spring MVC
- mysql - MySQL双左连接将结果加倍,这会改变列总和结果