I have a User Entity and a Record entity. I want to get a list that shows all users and their record list filtered by record's date. However, I got stuck with not being able to filter the result with conditions.
def room_version = "2.2.5"
implementation "androidx.room:room-runtime:$room_version"
@Entity(tableName = "user_table")
public class User {
@PrimaryKey public long id;
public String name;
}
@Entity(tableName = "record_table")
public class Record {
@PrimaryKey @ColumnInfo(name = "record_id")public long recordId;
@ColumnInfo(name = "user_id") public long userId;
public String date;
@ColumnInfo(name = "action")
public String action;
}
public class UserWithRecords {
@Embedded
protected User user;
@Relation(
parentColumn = "id",
entity = Record.class,
entityColumn = "user_id"
)
protected List<Record> records;
}
And here's all of the query I tried:
//in Dao
//first try:
@Transaction
@Query("SELECT * FROM user_table LEFT JOIN record_table ON id = user_id WHERE date=:date")
public LiveData<List<UserWithRecords>> getDailyRecord(String date);
//second try:
@Transaction
@Query("SELECT * FROM user_table LEFT JOIN (SELECT * FROM record_table WHERE date=:date) ON id = user_id")
public LiveData<List<UserWithRecords>> getDailyRecord(String date);
//third try:
@Transaction
@Query("SELECT * FROM user_table INNER JOIN record_table ON id = user_id WHERE date=:date")
public LiveData<List<UserWithRecords>> getDailyRecord(String date);
<User> <Record>
id | name record_id | user_id | action | date
------------ ----------------------------------------
1 | Alice 1 | 1 | walk |2020-10-05
2 | Ben 2 | 1 | jog |2020-10-05
3 | Chris 3 | 2 | bike |2020-10-05
4 | 1 | walk |2020-10-14
5 | 2 | jog |2020-10-14
filtering by 2020-10-05 and get results with all queries:
id | name | record_id | action | date
----------------------------------
1 | Alice| 1 | walk | 2020-10-05
1 | Alice| 2 | jog | 2020-10-05
1 | Alice| 4 | walk | 2020-10-14
2 | Ben | 3 | bike | 2020-10-05
2 | Ben | 5 | jog | 2020-10-14
3 | Chris|------------------------------------->3rd query don't have this row
how do I get something like with this? Is it possible to do it with one query?
id | name | record_id | action | date
-------------------------------------------
1 | Alice| 1 | walk | 2020-10-05
1 | Alice| 2 | jog | 2020-10-05
2 | Ben | 3 | bike | 2020-10-05