【发布时间】:2019-09-02 13:21:24
【问题描述】:
我正在使用 scala 2.11、slick 2.1.0 和 postgres 9.6。大版本升级是不可能的。
我有 2 个相同的表(从同一模型创建),我想做一个除连接(左外连接为空):
我的模型如下所示:
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) }
}
我要运行的 SQL 查询是:
SELECT * from previous.coffees PRV
LEFT JOIN current.coffees CUR
ON PRV.international_coffee_id = CUR.international_coffee_id
WHERE PRV.international_coffee_id IS NULL;
我能做到:
for {
(c,s) <- Coffees.tableQuery(previousSchema).leftJoin(
Coffees.tableQuery(currentSchema)
) on((x,y) => x.internationalCoffeeId === y.internationalCoffeeId)
} yield(c)
这似乎给了我一个左外连接,但我在过滤空值服务器端时遇到了问题(添加 WHERE PRV.international_coffee_id IS NULL)
如有任何建议或想法,我将不胜感激。
【问题讨论】:
标签: sql postgresql scala slick