Pablo Fernandez
Pablo Fernandez

Reputation: 105220

"Select In" using Slick

Is there a way to perform a query like this in Slick:

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

Thanks

Upvotes: 5

Views: 853

Answers (2)

Martin Kolinek
Martin Kolinek

Reputation: 2010

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

this produces

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

which is equivalent in terms of rows returned. If you need that exact query for some reason you'll probably need to either extend slick or use a static query.

Upvotes: 4

barczajozsef
barczajozsef

Reputation: 483

yes:

the imports:

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

import Q.interpolation

the result, and the conversion to the result:

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

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

your query:

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)

Upvotes: 1

Related Questions