I have 2 tables: Main and Update with columns name, id and department. I would like to read the Main table first, and if the department field value is empty, i could like to read it from Update table.
For example, from the below table, I should get the department value for KING from Update table.
How to achieve this using scala slick with out running plain sql queries?
Main
+-------+----+------------+
+ name | id | department +
+-------+----+------------+
| KING | 10 | |
| BLAKE | 30 | SALES |
+-------+----+------------+
Update
+-------+----+------------+
+ name | id | department +
+-------+----+------------+
| KING | 10 | SYSTEMS |
| BLAKE | 30 | SALES |
+-------+----+------------+
Here is the plain sql query
SELECT
m.`name`,
m.`id`,
if(m.`department`='', u.`department`) as `department`
FROM `Main` m
FROM `Update` u ON m.id=u.id
I have following code defined so far ...
case class Main(name: Option[String],
id: Option[int],
department: Option[String])
case class Update(name: Option[String],
id: Option[int],
department: Option[String])
lazy val mainQuery = TableQuery[MainTableDefinition]
lazy val updateQuery = TableQuery[UpdateTableDefinition]
class MainTableDefinition(tag: Tag) extends Table[Main](tag, "Main") {
def name = column[Option[String]]("name")
def id = column[Option[int]]("id")
def department = column[Option[String]]("department")
override def * =
(name, id, department)<> (Main.tupled, Main.unapply)
}
class UpdateTableDefinition(tag: Tag) extends Table[Update](tag, "Update") {
def name = column[Option[String]]("name")
def id = column[Option[int]]("id")
def department = column[Option[String]]("department")
override def * =
(name, id, department)<> (Update.tupled, Update.unapply)
}
val query = for {
m <- mainQuery
u <- updateQuery if m.id === u.id
} yield (m, u)
db.run(query.to[List].result)
coalesce(postgresql.org/docs/9.2/static/functions-conditional.html) and use itval query = for { m <- mainQuery u <- updateQuery if m.id === u.id department = coalesce(m.department, u.department) } yield (m.id, department)- Arseniy Zhizhelev