How to get multiple counts from room database?

大憨熊 提交于 2021-01-28 09:00:56

问题


I want to return multiple counts from Room Select query in 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. This can be easily done using SQLite but I want to use it in room.


回答1:


You can give names to the counts and return them as a class, like:

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
}


来源:https://stackoverflow.com/questions/62382612/how-to-get-multiple-counts-from-room-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!