问题
I'm trying to get the max version number from a table. My table contents are:
id externalId name version
1 10 n1 1
2 65 n2 2
3 10 n3 2
4 77 n4 1
In the table above, we have rows that has the maximum version as 2, so my query has to return this maximum version row for the given externalId that I pass into my query.
I'm now stuck up with writing the Slick version of it. Any suggestions, pointers? This is what I have so far: (I pass in the externalId)
for {
myTable1 <- myTable1Elems
if myTable1.version === (
myTable1Elems
.filter(_.externalId === s"$externalId")
.map(_.version)
.max
)
} yield myTable1
This seems to be doing the wrong thing, as my tests show that it returns 2 rows when I ask the maximum version for externalId = 77. I get both
1 10 n1 1
4 77 n4 1
returned as results which definitely is wrong. I guess I need to groupBy, but I just could not figure out how I could write the Slick combinator! Any help?
回答1:
So I managed to get this solved:
private def configMaxVersionFilter(extId: String) = for {
tbl <- myTable1Elems
if tbl.externalId === s"$extId" && tbl.version === (
myTable1Elems
.filter(_.externalId === s"$extId")
.map(_.version)
.max
)
} yield tbl
来源:https://stackoverflow.com/questions/30407289/slick-2-1-0-filter-max-version-in-a-table