简体   繁体   English

如何从房间数据库中获取多个计数?

[英]How to get multiple counts from room database?

I want to return multiple counts from Room Select query in android .我想从 android 中的Room android查询返回多个计数。 My query is like我的查询就像

Select Count(CASE WHEN x = '0' or x = '2'), Count(Case when a = '33' or a = '23') FROM my_table WHERE id=10

I want above query to return something as list which will contain values of both the above Count() function.我希望上面的查询以列表的形式返回一些内容,其中将包含上述Count() function 的值。 This can be easily done using SQLite but I want to use it in room .这可以使用SQLite轻松完成,但我想在room中使用它。

You can give names to the counts and return them as a class, like:您可以为计数命名并将它们作为 class 返回,例如:

data class MyCounts(
  @ColumnInfo(name = "first_count")
  val firstCount: Int,

  @ColumnInfo(name = "second_count")
  val secondCount: Int
)

@Dao
interface MyDao {
  @Query("SELECT COUNT(*) as first_count, COUNT(*) as second_count from my_table")
  suspend fun getMyCounts(): MyCounts
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM