在 Scala slick 中选择 DISTINCT [英] SELECT DISTINCT in Scala slick

查看:39
本文介绍了在 Scala slick 中选择 DISTINCT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Slick 1,我必须能够在查询中应用过滤器来查找与相关表中的条件匹配的所有实体.

I am using Slick 1, and I have to be able to apply a filter in a query to lookup all entities that match a condition in a related table.

这个使用 Slick 文档的示例显示了我正在尝试做的事情(这是一个接近我的情况的人为示例).

This example using the Slick documentation shows what I am trying to do (this is a contrived example that is close to my situation).

这里,我想要西海岸供应商提供的所有咖啡.我只想要 Coffee,我只对导航到供应商以应用过滤器感兴趣:

Here, I want all coffees that are provided by suppliers on the west coast. I want the Coffee only, I am only interested in navigating to Suppliers to apply the filter:

val westCoast = Seq("CA", "OR", "WA")
val implicitInnerJoin = for {
  c <- Coffees
  s <- Suppliers if c.supID === s.id && s.state inSet westCoast
} yield c

这没问题,但如果供应商表中有多个匹配项,它会复制 Coffees.

This works ok, but it will duplicate Coffees if there is more than one match in the Suppliers table.

明显的解决方法是在普通 SQL 中执行 SELECT DISTINCT;但是,我在这里找不到方法.

The obvious workaround is in normal SQL to do a SELECT DISTINCT; however, I cannot find a way to do that here.

理论上你可以这样做:

query.list.distinct

结果已经返回后;但是,我还实现了 PAGING 支持,因此一旦已经从数据库中返回结果,您就不会想要处理结果.这是分页支持:

After the results are already returned; however, I have also implemented PAGING support, so you wouldn't want to process the results once the already come back from the database. Here is the paging support:

query.drop(offset).take(limit).list

所以,简而言之,我需要一种在查询中指定 SELECT DISTINCT 的方法.

So, in a nutshell, I need a way to specify SELECT DISTINCT in my query that goes out.

有人有什么想法吗?

推荐答案

作为变通方法,您可以尝试使用 groupBy:

As a work around you can try to use groupBy:

query.groupBy(x=>x).map(_._1)

它应该具有与 distinct 相同的语义,但我不确定性能.

It should have the same semantics as distinct, but I'm not sure about performance.

这篇关于在 Scala slick 中选择 DISTINCT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆