我有两个表,我想得到一个 Java 对象。
这是我的第一个表:
@PrimaryKey(autoGenerate = false)
@ColumnInfo(name = "id")
long id;
@ColumnInfo(name = "date")
String date;
@ColumnInfo(name = "azimuth")
int azimuth;
这是我的第二张 table :
@PrimaryKey(autoGenerate = false)
@ColumnInfo(name = "id")
long id;
@ColumnInfo(name = "name")
String name;
@ColumnInfo(name = "symbol")
String symbol;
@ColumnInfo(name = "type")
String type;
表 1 中的 ID 与表 3 中的 ID 相同
我想查询其中有一个此类:
long id;
String date;
int azimuth;
String name;
String symbol;
String type;
最佳答案
首先,创建一个包含所需字段的数据类 ResultTable
:
public class ResultTable{
long id;
String date;
int azimuth;
String name;
String symbol;
String type;
}
现在可以查询两个表的数据,将结果放入上表,如下图;
@Query("SELECT id, t1.date as date, t1.azimuth as azimuth, t2.name as name, t2.symbol as symbol, t2.type as type FROM tabel1 t1, table2 t2 WHERE t1.id = t2.id AND t1.id = :id")
List<ResultTable> performJoinQuery(long id)
关于Java android room 从两个表中获取java对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56593728/