2013-03-21 70 views
5

有没有在油滑执行这样的查询方式:“选择在” 用油滑

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

感谢

+0

这是一个类似的问题:http://stackoverflow.com/questions/14920153/how-to-write-nested-queries-in-select-clause – 2013-03-22 00:39:48

回答

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)

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') 

其是行的方面等效返回。如果由于某种原因需要确切的查询,则可能需要扩展浮动或使用静态查询。