有没有办法在 Slick 中执行这样的查询:
"select * from foo where id IN (select other_id from bar where status = 'damaged')"
谢谢
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')
就返回的行而言,这是等效的。如果出于某种原因您需要精确的查询,您可能需要扩展 slick 或使用静态查询。
是的:
进口:
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)