【问题标题】:How to write nested queries in select clause with slick 3.2 +如何使用 slick 3.2 + 在 select 子句中编写嵌套查询
【发布时间】:2018-07-09 08:46:19
【问题描述】:

有没有办法使用 slick 3.2+ 创建嵌套选择? 基本上我需要在这里描述的所有内容How to write nested queries in select clause

但是在 slick 3.2 上,这种方法不起作用。

【问题讨论】:

    标签: scala slick


    【解决方案1】:

    如果你有表Users(id:UUID,email:String)和Persons(userId:UUID,name:String,surname:String)而不是查询

    select email
    from Users
    where id in (select userId
                 from Persons
                 where name = 'John'
                 and surname = 'Smith')
    

    看起来有点像:

    users
      .filter(
        _.id in persons
                  .filter(p => p.name === "John" && p.surname === "Smith")
                  .map(_.userId)
      )
      .map(_.email)
      .result
    

    你需要记住的事情:

    • Query 类型不是 DBIO(也不是 DBIOAction) - 如果你想编写查询,你需要在调用 .result 之前完成它
    • 当您在条件中使用子查询时,使用in 而不是inSet

    无论您使用injoin 等,都应遵循相同的原则。

    【讨论】:

    • 谢谢,但我需要嵌套选择,即。 select p.1, (select a.2 from tableA a where p.id = a.id ) from tableA as p
    • 你可以重写它:( tableA.map(a => (a.id, a.2)) join tableB.map(b.id, b.1) on (_._1 === _._1) ).map(_._4)。或者,只需使用sql"""query"""
    猜你喜欢
    • 2013-02-01
    • 2022-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-08
    • 2019-06-10
    相关资源
    最近更新 更多