Select In Using Slick

Is there a way to execute such a request in Slick:

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

thank

+5
source share
2 answers
for{
    f <- foo,
    b <- bar if (b.status === 'damaged' && f.id === b.other_id)
} yield f

this gives

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

which is equivalent in terms of returned strings. If you need an exact query for any reason, you may have to either stretch the stain or use a static query.

+4
source

Yes

import:

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

import Q.interpolation

result and conversion to result:

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

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

your request:

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)

+1
source

All Articles