使用Slick“选择”

时间:2013-03-21 23:00:40

标签: scala slick

有没有办法在Slick中执行这样的查询:

"select * from foo where id IN (select other_id from bar where status = 'damaged')"

由于

2 个答案:

答案 0 :(得分:4)

for{
    f <- foo,
    b <- bar if (b.status === 'damaged' && f.id === b.other_id)
} yield f

这会产生

select x2."id" from "foo" x2, "bar" x3 
    where (x2."id" = x3."other_id") and (x3."status" = 'damaged')

在返回的行方面是等效的。如果出于某种原因需要精确查询,则可能需要扩展光滑或使用静态查询。

答案 1 :(得分:1)

是:

进口:

import scala.slick.jdbc.{ GetResult, StaticQuery => Q }

import Q.interpolation

结果,并转换为结果:

case class FooResult(id: Int, name: String)

implicit val getPersonResult = GetResult(r => FooResult(r.<<, r.<<))

您的查询:

val status = "damaged"

val q = Q.query[String,FooResult]("select * from foo where id IN (select other_id from bar where status = ?)")

val result = q.list(status)