首页 > 解决方案 > 如何修复 SQLException:错误:“:”位置或附近的语法错误:15?

问题描述

我想获取以下问题中提到的数据。

查询以每小时检索计数,如果没有则检索为零

所以据此,我在 Postgresql 中执行以下查询,所以它工作正常。

select '00:00'::time + g.h * interval '1 hour',count(sv.id) as orders from generate_series(0, 23, 1) g(h) left join paymentvirtualization.summery_virtualizer sv on extract(hour from sv.last_updated) = g.h and date_trunc('day', sv.last_updated) = '2019-10-11' group by g.h order by g.h;

但我的问题是我希望它像 Spring Data JPA 本机查询一样执行。

  @Query(
            value = "select '00:00'::time + g.h * interval '1 hour',count(sv.id) as orders from generate_series(0, 23, 1) g(h) left join paymentvirtualization.summery_virtualizer sv on extract(hour from sv.last_updated) = g.h and date_trunc('day', sv.last_updated) = '2019-10-11' group by g.h order by g.h",
            nativeQuery = true
    )
    List<Map<String, Integer>> getPaymentDistry();

如果我执行它,我会收到类似的错误

could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet"

有人能帮我一下吗?

谢谢,

标签: postgresqlspring-bootspring-data-jpa

解决方案


混淆层阻塞,::因为它用于:引入命名参数。通常,解决方案是改用符合标准的cast()运算符。

select cast('00:00' as time) + g.h * interval '1 hour' ...

或者使用 Laurenz 建议的 ANSI 时间语法:

select time '00:00' + g.h * interval '1 hour' ...

推荐阅读