Scala Slick 过滤并加入
Scala Slick filter and join
在 Slick 中执行过滤器连接时,以下两种方法之间有什么区别?
val query = for {
c <- coffees if c.price < 9.0
s <- c.supplier -- assuming there is a foreign key
} yield (c.name, s.name)
和
val query = for {
(cof, sup) <- coffees.filter(_.price < 9.0) join supplier on(_.supId === _.id)
} yield (cof.name, sup.name)
第一个是隐式连接,第二个是显式连接。 Slick 为前者生成一个 WHERE
子句,如:WHERE c.price < 9 AND c.supId = s.id
。然而,后者会生成 JOIN
,如 JOIN supplier s ON c.supId = s.id
。你可以看看these examples.
在 Slick 中执行过滤器连接时,以下两种方法之间有什么区别?
val query = for {
c <- coffees if c.price < 9.0
s <- c.supplier -- assuming there is a foreign key
} yield (c.name, s.name)
和
val query = for {
(cof, sup) <- coffees.filter(_.price < 9.0) join supplier on(_.supId === _.id)
} yield (cof.name, sup.name)
第一个是隐式连接,第二个是显式连接。 Slick 为前者生成一个 WHERE
子句,如:WHERE c.price < 9 AND c.supId = s.id
。然而,后者会生成 JOIN
,如 JOIN supplier s ON c.supId = s.id
。你可以看看these examples.