Scala slick in/contains的实现方法

参考:https://scala-slick.org/doc/3.1.1/sql-to-slick.html

 

Subquery

SQL

sql"""
  select *
  from PERSON P
  where P.ID in (select ID
                 from ADDRESS
                 where CITY = 'New York City')
""".as[Person]

Slick

Slick queries are composable. Subqueries can be simply composed, where the types work out, just like any other Scala code.

val address_ids = addresses.filter(_.city === "New York City").map(_.id)
people.filter(_.id in address_ids).result // <- run as one query

The method .in expects a sub query. For an in-memory Scala collection, the method .inSet can be used instead.

posted @ 2020-10-07 20:02  梦醒江南·Infinite  阅读(186)  评论(0编辑  收藏  举报