我正在尝试获取一张地图,该地图基本上是Map,并且具有来自JOOQ的书本和计数信息。
我尝试了以下和不同的组合,但始终会出错。任何帮助,将不胜感激:

    Map<String, Result<Record1<Integer>>> countMap = ctx.selectCount().from(BOOKTABLE)
         .groupBy(BOOKTABLE.TYPE)
         .fetchMAP(BOOKTABLE.TYPE);

最佳答案

在这种情况下,您不能使用selectCount(),因为当您确实需要SELECT COUNT(*)查询时,它将产生一个SELECT type, COUNT(*)查询。

这有几种方法,具体取决于您要从查询中获取哪种类型:

// Assuming this static import:
import static org.jooq.impl.DSL.*;

Map<String, Integer> map1 =
ctx.select(BOOKTABLE.TYPE, count())
   .from(BOOKTABLE)
   .groupBy(BOOKTABLE.TYPE)
   .fetchMap(BOOKTABLE.TYPE, count());


要么:

Map<String, Record2<String, Integer>> map2 =
ctx.select(BOOKTABLE.TYPE, count())
   .from(BOOKTABLE)
   .groupBy(BOOKTABLE.TYPE)
   .fetchMap(BOOKTABLE.TYPE);


要么:

Map<String, List<Integer>> map3 =
ctx.select(BOOKTABLE.TYPE, count())
   .from(BOOKTABLE)
   .groupBy(BOOKTABLE.TYPE)
   .fetchGroups(BOOKTABLE.TYPE, count());


要么:

Map<String, Result<Record2<String, Integer>>> map4 =
ctx.select(BOOKTABLE.TYPE, count())
   .from(BOOKTABLE)
   .groupBy(BOOKTABLE.TYPE)
   .fetchGroups(BOOKTABLE.TYPE);

关于java - JOOQ从selectCount检索 map ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36732569/

10-10 11:28