我有这个简单的实体

@Entity
data class WebpageVisit(
    @PrimaryKey(autoGenerate = true) val id: Long?,
    val url: String,
    val domain: String,
    val visitTimestamp: Long,
    val visitsCount: Int)

我正在尝试运行这个简单的查询
    @Query("SELECT *, count(*) as visitsCount from webpagevisit group by domain order by visitsCount desc")
    abstract fun getTopWebsites(): LiveData<List<WebpageVisit>>

问题是结果没有排序。数据正确分组在一起,visitsCount值正确,但是数据未排序。

感谢您的任何提示。

最佳答案

您必须以所需的方式定义顺序,例如ASC或DESC。
对于升序,

SELECT Column1,Column2, count(*) as visitsCount from webpagevisit group by domain order by count(*) ASC

对于降序,
SELECT Column1,Column2, count(*) as visitsCount from webpagevisit group by domain order by count(*) DESC

关于android - Room db-按别名排序无法在分组依据查询中使用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/60080924/

10-09 13:03