gpt4 book ai didi

mysql - 在 Slick 中表达 SQL IF ELSE

转载 作者:行者123 更新时间:2023-11-29 09:48:14 24 4
gpt4 key购买 nike

例如,我如何在 Slick 中表达这个 SQL?

select column as true / false if id is exists in another table

join(Services).on(_.mid === _.id).map {
case (m, s) => (m.id, m.name, ???).mapTo[SomeClass]
}

最佳答案

尝试

  import slick.jdbc.PostgresProfile.api._

case class Member(id: Int, name: String, phone: String)

class MemberTable(tag: Tag) extends Table[Member](tag, "members") {
def id = column[Int]("id", O.PrimaryKey)
def name = column[String]("name")
def phone = column[String]("phone")
def * = (id, name, phone) <> (Member.tupled, Member.unapply)
}

case class Service(mid: Int, profile: Int, lastSeen: String)

class ServiceTable(tag: Tag) extends Table[Service](tag, "services") {
def mid = column[Int]("mid", O.PrimaryKey)
def profile = column[Int]("profile")
def lastSeen = column[String]("last_seen")
def * = (mid, profile, lastSeen) <> (Service.tupled, Service.unapply)
}

val members = TableQuery[MemberTable]
val services = TableQuery[ServiceTable]
case class SomeClass(mid: Int, name: String, services: Boolean)

members.joinLeft(services).on(_.id === _.mid).map {
case (m, s) => (m.id, m.name, s.isDefined).mapTo[SomeClass]
}

关于mysql - 在 Slick 中表达 SQL IF ELSE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55285069/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com