如何从房间数据库中获取多个计数? [英] How to get multiple counts from room database?

查看:72
本文介绍了如何从房间数据库中获取多个计数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要从 Room 返回多个计数.在 android 中选择查询.我的查询就像

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

我希望以上查询返回一些内容作为列表,其中将包含上述两个 Count()函数的值.使用 SQLite 可以轻松完成此操作,但是我想在 room 中使用它.

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.

推荐答案

您可以为计数指定名称,并将其作为类返回,例如:

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
}

这篇关于如何从房间数据库中获取多个计数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆