在PostgreSQL中我想current_timestamp用interval以下内容连接:
select current_timestamp + interval 2||' days'
Run Code Online (Sandbox Code Playgroud)
但是当我这样做时,我收到一个错误:
[Err] ERROR: syntax error at or near "2"
LINE 1: select current_timestamp + interval 2||' days'
Run Code Online (Sandbox Code Playgroud)
但如果我这样做,它可以正常工作:
select current_timestamp + interval '2 days'
Run Code Online (Sandbox Code Playgroud)
为什么一个工作,而另一个工作?
参考以下页面 http://www.postgresql.org/docs/8.0/static/functions-datetime.html
我正在使用 Oracle 数据库。我需要通过 jpa 存储库运行更新查询。这是我尝试执行的查询。
@Transactional(propagation = Propagation.REQUIRES_NEW)
@Modifying
@Query(
value = "UPDATE transactionlog SET transactionstatus= :ps,startedat = CURRENT_TIMESTAMP, readytoprocessat= (CURRENT_TIMESTAMP+ interval ':to' second) WHERE logid IN (:li) ",
nativeQuery = true)
public Integer reserve(@Param("ps") short processingStatus, @Param("li") List<Integer> logIdList, @Param("to") int timeOut);
Run Code Online (Sandbox Code Playgroud)
但这个例外
org.springframework.dao.InvalidDataAccessApiUsageException: Parameter with that name [to] did not exist; nested exception is java.lang.IllegalArgumentException: Parameter with that name [to] did not exist
Run Code Online (Sandbox Code Playgroud)
但如果我按如下方式更改此方法,它就可以正常工作。
@Transactional(propagation = Propagation.REQUIRES_NEW)
@Modifying
@Query(
value = "UPDATE transactionlog SET transactionstatus= :ps,startedat = CURRENT_TIMESTAMP, …Run Code Online (Sandbox Code Playgroud) 我需要将动态参数传递给 Spring Data Postgres 本机查询。参数位于区间表达式中。
在运行代码时,我从 pgAdmin 验证以下内容仅返回 1 个结果(正确):
select * from recalls_t where created_date <= (now() - interval '18 hour')
问题:
1)在Spring Data代码中,以下?1表示法错误地返回2个结果:
@Query(value="select * from recalls_t where created_date <= (now() - interval '?1 hour')",
nativeQuery=true)
public List<RecallsT> findActiveRecallsInLastHrs(@Param("hours") int hours);
Run Code Online (Sandbox Code Playgroud)
2)然后我尝试了这个线程中的解决方案:Postgres Interval not work with native spring data JPA query
他们说要使用单个单位间隔的倍数。但这也会错误地返回 2 个结果:
@Query(value="select * from recalls_t where created_date <= (now() -
(interval '1 hour') * :hours)",
nativeQuery=true)
public …Run Code Online (Sandbox Code Playgroud) 我创建了一个带间隔的本地查询。当我day在查询中进行硬编码时,查询工作正常:
@Query(value="select * from orders where created_date < clock_timestamp() - interval ' 5 days'",nativeQuery=true)
Run Code Online (Sandbox Code Playgroud)
但是当我提供这样的数据时@Param:
@Query(value="select * from orders where created_date < clock_timestamp() - interval :day 'days'",nativeQuery=true)
List<Order> getData(@Param("day") String day)
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
由以下原因引起:org.postgresql.util.PSQLException:错误:“ $ 1”或附近的语法错误