在 Scala slick 中选择 DISTINCT

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18256768/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 05:35:33  来源:igfitidea点击:

SELECT DISTINCT in Scala slick

scalaslick

提问by noplay

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 1,并且我必须能够在查询中应用过滤器来查找与相关表中的条件匹配的所有实体。

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

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

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:

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

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

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

这工作正常,但如果供应商表中有多个匹配项,它将复制 Coffees。

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

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

You could in theory do a:

理论上你可以做一个:

query.list.distinct

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:

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

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

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

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

Anyone have any ideas?

有人有想法么?

采纳答案by Martin Kolinek

As a work around you can try to use groupBy:

作为解决方法,您可以尝试使用 groupBy:

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

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

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

回答by Valerii Rusakov

With slick 3.1.0 you can use distinctand distinctOnfunctions (Slick 3.1.0 release notes). For example:

使用 slick 3.1.0,您可以使用distinctdistinctOn运行(Slick 3.1.0 发行说明)。例如:

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

db.run(implicitInnerJoin.distinctOn(_.name).result)

回答by Eugene Ryzhikov

I don't think this implemented yet. See https://github.com/slick/slick/issues/96

我认为这还没有实施。见https://github.com/slick/slick/issues/96

回答by binshi

For distinct on multiple columns coffee.name and coffee.price:

对于多个列上的不同,coffee.name 和 coffee.price:

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

db.run(implicitInnerJoin.map(f => (f.name, f.price, f.state)).distinctOn(p => (p._1, p._2)).result)