Slick 2聚合-如何获得标量结果? [英] Slick 2 aggregation - how to get a scalar result?
问题描述
我有一个表,其中有一个Int
列TIME
:
I have a table with an Int
column TIME
in it:
def time = column[Int]("TIME")
表已映射为自定义类型.我想找到最大时间值,即执行简单的汇总. 文档中的示例似乎很容易:
The table is mapped to a custom type. I want to find a maximum time value, i.e. to perform a simple aggregation. The example in the documentation seems easy enough:
val q = coffees.map(_.price)
val q1 = q.min
val q2 = q.max
但是,当我这样做时,q1和q2的类型是Column[Option[Int]]
.我可以对此执行get
或getOrElse
以获得Column[Int]
类型的结果(即使这对我来说似乎有些令人惊讶-是get
的Column
成员,还是从Int
,然后再次包装到Column?为什么?),但是我无法使用标量值,当我尝试将其分配给Int时,出现一条错误消息:
However, when I do this, the type of q1 and q2 is Column[Option[Int]]
. I can perform a get
or getOrElse
on this to get a result of type Column[Int]
(even this seems somewhat surprising to me - is get
a member of Column
, or is the value converted from Option[Int]
to Int
and then wrapped to Column again? Why?), but I am unable to use the scalar value, when I attempt to assign it to Int, I get an error message saying:
type mismatch;
found : scala.slick.lifted.Column[Int]
required: Int
如何从聚合查询中获取scala值?
How can I get the scala value from the aggregated query?
推荐答案
我的猜测是您没有调用调用程序,这就是为什么您获得Column
对象的原因.试试这个:
My guess is that you are not calling the invoker that's the reason why you get a Column
object. Try this:
val q1 = q.min.run
应返回Option[Int]
,然后您可以get
或getOrElse
.
Should return an Option[Int]
and then you can get
or getOrElse
.
这篇关于Slick 2聚合-如何获得标量结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!