2

我想使用 dbFlow 选择最常用的名称。在 SQLite 中,它将是:

SELECT `Employee`.name
FROM `Employee`
GROUP BY `Employee`.name
HAVING COUNT(*) =
  (SELECT MAX(cn) FROM
     (SELECT `Employee`.name, COUNT(*) AS cn
      FROM `Employee`
      GROUP BY `name`))

我试过了:

        SQLite.select(Employee_Table.name)
                .from(Employee.class)
                .groupBy(Employee_Table.name)
                .having(Method.count().eq(
                        SQLite.select(
                                Method.max("cn")).from(
                                SQLite.select(Employee_Table.name, Method.count().as("cn"))
                                        .from(Employee.class)
                                        .groupBy(Employee_Table.name)
                        )
                        )
                )

但是 Method.max 不接受别名。或者我应该只运行一个原始查询?

4

1 回答 1

1

Method.max 只接受你可以创建的属性。

public static Property<Long> name(String value) {
    return new Property<Long>(null, value) {
        @Override
        public String toString() {
            return nameAlias.nameRaw();
        }
    };
}

用法:

Method.max(name("cn"))
于 2017-08-17T14:08:20.313 回答