【问题标题】:How to pass limit and off set values dynamically in native query如何在本机查询中动态传递限制和偏移值
【发布时间】:2019-07-15 10:53:43
【问题描述】:

我正在使用本机查询从 PostgreSQL 获取分页结果,我使用了这个查询,我得到了以下异常:

SELECT a.*
  FROM table1 a LEFT OUTER JOIN table2 b ON a.clmn1 = b.clmn1
  WHERE (a.clmn3 = ?3 OR a.clmn4 ISNULL)
  ORDER BY a.clmn1 DESC offset = ?1 limit = ?2

查询:

@Query(nativeQuery = true, value="select a.* from table1 a left outer join table2 b ON a.clmn1 = b.clmn1 where (a.clmn3= ?3 OR a.clmn4 isnull) order by a.clmn1 desc offset = ?1 limit = ?2")
public List<Result> getResults(int offset, int limit, int value);

例外:

org.postgresql.util.PSQLException: ERROR: syntax error at or near "="

请提出建议。

【问题讨论】:

  • 你在 is 和 null 之间缺少一个空格..
  • 不是同样的问题
  • 重组了上面的查询,指出“is”和“null”之间缺少空格...OR a.clmn4 ISNULL

标签: java postgresql spring-data-jpa eclipselink nativequery


【解决方案1】:

您的查询中有语法错误。删除 = 并首选命名参数,例如:

@Query(nativeQuery = true, value = "SELECT a.* FROM table1 a "
    + " LEFT OUTER JOIN table2 b"
    + " ON a.clmn1 = b.clmn1 "
    + " WHERE (a.clmn3= ?3 OR a.clmn4 IS NULL) "
    + " ORDER BY a.clmn1 DESC OFFSET :offset LIMIT :limit ")
public List<Result> getResults(@Param("offset")int offset, 
                               @Param("limit")int limit, int value);

免责声明:没有测试参数注入是如何工作的,但语法应该是这样的

【讨论】:

    【解决方案2】:

    我相信错误是

    offset = ?1 limit = ?2
    

    https://www.postgresql.org/docs/8.0/queries-limit.html

    偏移量和限制不需要'='

    【讨论】:

      猜你喜欢
      • 2017-11-17
      • 2018-10-05
      • 1970-01-01
      • 2012-05-17
      • 2019-06-06
      • 2016-09-19
      • 2013-01-30
      • 1970-01-01
      • 2021-04-02
      相关资源
      最近更新 更多