解决方法
假设您的表定义如下所示:
import slick.driver.PostgresDriver.api._ // Import your driver here class EmployeesTable(tag: Tag) extends Table[(Option[Int],Int)](tag,"employees") { def firstCol = column[Option[Int]]("first_col") // This column is nullable def secondCol = column[Int]("second_col") // This column is non-nullable def * = (firstCol,secondCol) }
然后您的查询可能如下所示:
val query = TableQuery[EmployeesTable].map(employee => employee.firstCol.ifNull(employee.secondCol)) val result: Future[Seq[Int]] = db.run(query.result)
这样,first_col中的每个空值都将被second_col中的值替换.这将等同于以下SQL查询:
select coalesce("first_col","second_col") from "employees"