android - 如何在 roomdb 查询中使用 where 关键字
问题描述
@Entity
data class User(
@PrimaryKey val userId: Long,
val name: String,
val age: Int
)
@Entity
data class Playlist(
@PrimaryKey val playlistId: Long,
val userCreatorId: Long,
val playlistName: String
)
ata class UserWithPlaylists(
@Embedded val user: User,
@Relation(
parentColumn = "userId",
entityColumn = "userCreatorId"
)
val playlists: List<Playlist>
)
@Transaction
@Query("SELECT * FROM User")
fun getUsersWithPlaylists(): List<UserWithPlaylists>
这个例子来自https://developer.android.com/training/data-storage/room/relationships ,我的问题
如果我只想让用户年龄=10
@Query("SELECT * FROM User where age=10")
fun getUsersWithPlaylists(): List<UserWithPlaylists>
但如果我想要 playlistName ="quran"
@Query("SELECT * FROM User where playlistName=quran")
fun getUsersWithPlaylists(): List<UserWithPlaylists>
我不能这样做,因为roomdb看不到播放列表表
解决方案
Join
您需要的功能在 SQL 数据库中调用。我建议您使用一些教程来学习。这是根据您的结构的答案。
SELECT `user`.`UserId`,`user`.`name`,`user`.`age` From `user` INNER JOIN `playlist` On `playlist`.`userCreatorId`=`user`.`UserId` WHERE `playlist`.`playlistName`='Use your playlist name here'
这里有一篇关于Join
.
推荐阅读
- python - 使用美丽的汤选择 css 选择器
- c++ - 使用 GetAdaptersAddresses() 获取 IPv6 DNS 服务器地址
- mysql - 使用 getMany() 获取具有 limit 和 order by 以及子关系的 TypeORM 数据
- tensorflow - 为什么 multiscale_anchor_generator 中设置 normalize_coordinates 为 false 时检测模型的评估指标(mAP)总是 0?
- c# - 如何在 C# 上通过递归获取具有多个扩展名的文件?
- powerbi - 使用 Power Query 编辑器加载表数据后出现超时错误。我该如何进一步进行?
- javascript - 为什么我在使用这个关键字时变得不确定
- java - 有没有办法使用java从本地系统上传文件到EFS?
- c# - .Net Core webApi 在运行时将属性添加到传入的 Dto
- git - 如何在格式化的 git 提交日期输出上将时区更改为本地?