5

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

Pablo Fernandez
  • 103,170
  • 56
  • 192
  • 232
  • This is a similar question: http://stackoverflow.com/questions/14920153/how-to-write-nested-queries-in-select-clause – Alex Yarmula Mar 22 '13 at 00:39

2 Answers2

4
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.

Martin Kolinek
  • 2,010
  • 14
  • 16
1

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)

barczajozsef
  • 473
  • 5
  • 11