Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Select In" using Slick

Tags:

scala

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

like image 955
Pablo Fernandez Avatar asked Mar 21 '13 23:03

Pablo Fernandez


2 Answers

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.

like image 85
Martin Kolinek Avatar answered Nov 05 '22 08:11

Martin Kolinek


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)

like image 30
barczajozsef Avatar answered Nov 05 '22 08:11

barczajozsef