1

如何在 Exposed 中使用 QueryAlias 从 ResultRow 映射 count() 值
或者我做错了什么?

val countAlias = EventTable.join(AttendeeEventTable, JoinType.INNER)
    .slice(EventTable.id, EventTable.id.count())
    .selectAll()
    .groupBy(EventTable.id)
    .alias("countAlias")

EventTable
    .join(countAlias, JoinType.LEFT, EventTable.id, countAlias[EventTable.id])
    .selectAll()
    .map {
         Event(
               id = it[EventTable.id].value,
               name = it[EventTable.name],
               countOfAttendeees = it[countAlias[EventTable.id.count()]],  //Not working. How to get the count() from ResultRow
              )
         }

4

1 回答 1

1

只需在查询之前定义一个计数“列”。这是一个例子:

        val count = FooTable.status.count().alias("count")

        FooTable
            .slice(FooTable.status, count)
            .selectAll()
            .groupBy(FooTable.status)
            .map {
                FooCount(
                    it[FooTable.status],
                    it[count]
                )
            }

如您所见,我创建了一个值count,然后在slice选择中使用它,然后在行映射中使用它。

于 2020-04-13T11:25:35.497 回答