2013-03-21 14 views
5

Czy istnieje sposób, aby wykonać kwerendę tak w Slick:"Wybierz" Korzystanie Slick

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

Dzięki

+0

To jest podobne pytanie: http://stackoverflow.com/questions/14920153/how-to-write-nested-queries-in-select-clause –

Odpowiedz

1

tak:

importuje:

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

import Q.interpolation

wynik i konwersja do wyniku:

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

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

zapytanie:

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 

ten produkuje

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

który jest równoważny pod względem zwracanych wierszy. Jeśli potrzebujesz tego dokładnego zapytania z jakiegoś powodu, prawdopodobnie będziesz musiał rozszerzyć działanie lub użyć statycznego zapytania.