我在Android Room中遇到SQL查询问题。我有如下查询
@Query("SELECT userId, COUNT(*) AS userCount FROM User " +
"GROUP BY userId " +
"ORDER BY userCount ASC")
List<UserCount> getUserAndCount();
但是Android Studio显示Cannot resolve symbol 'userCount'
。上面的语句在SQL中工作正常,您能告诉我代码中的问题是什么,以及如何在Android Room中使用AS
吗?
答案 0 :(得分:3)
从order by中删除别名,您需要按clase顺序使用COUNT(*)表达式
@Query("SELECT userId, COUNT(*) AS userCount FROM User " +
"GROUP BY userId " +
"ORDER BY COUNT(*)")
List<UserCount> getUserAndCount();