如何查询明天到期但得到 'syntax error ' 的东西

How to query for things that are due tomorrow but getting the 'syntax error '

我正在使用node-postgres想查询满足current_date条件的数据库是endDate的前一天,基本上是明天到期的东西,但是我出现以下错误:

error: operator does not exist: timestamp without time zone - integer

我的代码:

    const res = await client.query('SELECT * FROM "Booking" LEFT JOIN "User" ON ("Booking"."renter"="User"."id") WHERE "endDate" - 1 = current_date')

我也试了下面的,还是一样的错误:

     const res = await client.query('SELECT * FROM "Booking" LEFT JOIN "User" ON ("Booking"."renter"="User"."id") WHERE extract(EPOCH FROM "endDate" - 1) = current_date')

然后我尝试了:

const res = await client.query('SELECT * FROM "Booking" LEFT JOIN "User" ON ("Booking"."renter"="User"."id") WHERE date_trunc(endDate) = date_trunc(current_timestamp + INTERVAL "1 day")')

我收到错误

error: syntax error at or near ""1 day""

如果我去掉引号,我得到:

error: syntax error at or near "1"

"endDate" 不是您所期望的日期,而是时间戳,您不能从中减去整数。您可以减去一个间隔,但这不是您想要的。

你应该这样比较:

WHERE "endDate" >= CAST (current_date + 1 AS timestamp)
  AND "endDate" <  CAST (current_date + 2 AS timestamp)

该条件具有额外的优势,它可以由 "endDate" 上的索引支持。