Sql Scala Slick:如何进行except连接(左外部连接为null)?
我使用的是scala 2.11、slick 2.1.0和postgres 9.6。主要版本升级是不可能的 我有两个相同的表(从同一个模型创建),我想进行一个except联接(左外部联接为null): 我的模型如下所示:Sql Scala Slick:如何进行except连接(左外部连接为null)?,sql,postgresql,scala,slick,Sql,Postgresql,Scala,Slick,我使用的是scala 2.11、slick 2.1.0和postgres 9.6。主要版本升级是不可能的 我有两个相同的表(从同一个模型创建),我想进行一个except联接(左外部联接为null): 我的模型如下所示: trait Coffee { val coffeeLoversId: Option[Int] val taxId: Option[Long] val internationalCoffeeId: String
trait Coffee {
val coffeeLoversId: Option[Int]
val taxId: Option[Long]
val internationalCoffeeId: String
val providerId: String
}
class Coffees(tag: Tag, schema: String) extends Table[Coffee](tag, Some(schema), "coffees")
val coffeeLoversId: Column[Option[Int]] = column[Option[Int]]("coffee_lovers_id")
val taxId: Column[Option[Long]] = column[Option[Long]]("tax_id")
val internationalCoffeeId: Column[String] = column[String]("international_coffee_id", O.DBType("VARCHAR"))
val providerId: Column[String] = column[String]("provider_id", O.DBType("VARCHAR"))
def * = (coffeeLoversId, taxId, internationalCoffeeId, providerId) <> (Coffee.tupled, Coffee.unapply)
def ? = (coffeeLoversId, taxId, internationalCoffeeId.?, providerId.?).shaped.<>({r=>import r._; _1.map(_=> Coffee.tupled((_1, _2, _3.get, _4.get)))}, (_:Any) => throw new Exception("Inserting into ? projection not supported."))
}
object Coffees {
def tableQuery(implicit schema: TableSchema) = TableQuery[Coffees]{ tag : Tag => new Coffees(tag, schema.name) }
}
我能够做到以下几点:
for {
(c,s) <- Coffees.tableQuery(previousSchema).leftJoin(
Coffees.tableQuery(currentSchema)
) on((x,y) => x.internationalCoffeeId === y.internationalCoffeeId)
} yield(c)
用于{
(c,s)x.internationalCoffeeId==y.internationalCoffeeId)
}收益率(c)
这似乎给了我一个左外部联接,但我在筛选空服务器端时遇到了问题(添加其中PRV.international\u coffee\u id为空)
如果有任何建议或想法,我将不胜感激。我能够实现我的目标。首先,我尝试使用scala的条件理解:
for {
(c,s) <- Coffees.tableQuery(previousSchema).leftJoin(
Coffees.tableQuery(currentSchema)
) on((x,y) => x.internationalCoffeeId === y.internationalCoffeeId)
if(s.internationalCoffeeId.isEmpty)
} yield(c)
for {
(c,s) <- Coffees.tableQuery(previousSchema).leftJoin(
Coffees.tableQuery(currentSchema)
) on((x,y) => x.internationalCoffeeId === y.internationalCoffeeId)
if(s.internationalCoffeeId.isEmpty)
} yield(c)
for {
(c,s) <- Coffees.tableQuery(previousSchema).leftJoin(
Coffees.tableQuery(currentSchema)
) on(
(x,y) => x.internationalCoffeeId === y.internationalCoffeeId
).filter(_._2.internationalCoffeeId.isEmpty)
} yield(c)
val query = Coffees.tableQuery(previousSchema).leftJoin(
Coffees.tableQuery(currentSchema)
) on(
(x,y) => x.internationalCoffeeId === y.internationalCoffeeId
).filter(_._2.internationalCoffeeId.isEmpty)
query.selectStatement