如何在浮油中加入?

时间:2013-09-02 10:58:56

标签: scala slick

我有2个查询,想要像sql一样进行连接

例如我有这些表..

object Family extends Table[Long]("Family") {
  def id = column[Long]("id")
  def * = id
}## Heading ##
object Person extends Table[(Long, Long)]("Person") {
  def id = column[Long]("id")
  def FamilyId = column[Long]("FamilyId")
  def * = id ~ FamilyId ~ size
}

2 个答案:

答案 0 :(得分:4)

val implicitInnerJoin = for {
  c <- Family
  s <- Person if c.id === s.FamilyId
} yield (c.id, s.id)

答案 1 :(得分:1)

从Slick文档:http://slick.typesafe.com/doc/1.0.0/lifted-embedding.html#joining-and-zipping,下面是Slick中的一些可能的连接以及与之相关的一些粗略的SQL

val implicitCrossJoin = for {
  c <- Coffees
  s <- Suppliers
} yield (c.name, s.name)

//SELECT c.name, s.name FROM Coffees c, Suppliers s


val implicitInnerJoin = for {
  c <- Coffees
  s <- Suppliers if c.supID === s.id
} yield (c.name, s.name)
//SELECT c.name, s.name FROM Coffees c, Suppliers s where c.supID = s.id    

val explicitCrossJoin = for {
  (c, s) <- Coffees innerJoin Suppliers
} yield (c.name, s.name)
//SELECT c.name, s.name FROM Coffees c CROSS JOIN Suppliers s    

val explicitInnerJoin = for {
  (c, s) <- Coffees innerJoin Suppliers on (_.supID === _.id)
} yield (c.name, s.name)
//SELECT c.name, s.name FROM Coffees c INNER JOIN Suppliers s ON (c.supID = s.id)   

val explicitLeftOuterJoin = for {
  (c, s) <- Coffees leftJoin Suppliers on (_.supID === _.id)
} yield (c.name, s.name.?)
//SELECT c.name, s.name FROM Coffees c LEFT OUTER JOIN Suppliers s ON (c.supID = s.id)

val explicitRightOuterJoin = for {
  (c, s) <- Coffees rightJoin Suppliers on (_.supID === _.id)
} yield (c.name.?, s.name)
//SELECT c.name, s.name FROM Coffees c RIGHT OUTER JOIN Suppliers s ON (c.supID = s.id)

val explicitFullOuterJoin = for {
  (c, s) <- Coffees outerJoin Suppliers on (_.supID === _.id)
} yield (c.name.?, s.name.?)
//SELECT c.name, s.name FROM Coffees c FULL OUTER JOIN Suppliers s ON (c.supID = s.id)

正如您所看到的,大多数构造都非常直接地映射到SQL。

希望有所帮助