[英]Anorm replacement of params gives error on usage of aggregate functions
表結構 -
create table test
(month integer,
year integer,
thresholds decimal(18,2)
);
用於模擬的靜態插入 -
insert into test(month,year,threshold) values(4,2021,100),(5,2021,98),(6,2021,99);
如果我使用anorm
查詢postgres
數據庫,它適用於常規查詢。 但是,在添加max
等聚合函數時, RowParser
無法找到別RowParser
。
val queryString =
"""select max(month) as monthyear from test
| where (month || '-' || year)
| = {inQuery}""".stripMargin
val inQuery1 = "'5-2021'"
下面的on
方法導致問題-
val latestInBenchmark = SQL(queryString).on("inQuery" -> inQuery1) // removing the on resolves the problem
logger.info("query for latest period ---> " + latestInBenchmark)
val latestYearMonthInInterval = database.withConnection(implicit conn => {
latestInBenchmark.as(SqlParser.int("monthyear").*)
})
刪除on
糾正了問題, SqlParser.int(column-name)
按預期工作。 這也不影響使用count
聚合函數的查詢。
遇到錯誤:
(Validated intervals with sorting -> ,Failure(anorm.AnormException: 'monthyear' not found, available columns: monthyear, monthyear))
[error] c.b.ThresholdController - 'monthyear' not found, available columns: monthyear, monthyear
您遇到的錯誤有點誤導,但這意味着查詢要么返回具有空值的行或沒有行。
在您的情況下,我認為問題在於WHERE
子句:您在值周圍放置了單引號,但在使用.on(...)
或 Anorm 插值時,Anorm 會自行完成。
因此,替換:
val inQuery1 = "'5-2021'"
經過:
val inQuery1 = "5-2021"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.