0

I'm using GreenDAO in my project.

I have a chats table which contains all the messages. I want to group by the chat_id and order the messages in descending order by the timestamp of the message.

so what I'm trying to get is a list that contains the most recent message of each chat the user has.

this is the code I'm using:

Chats.queryBuilder()
     .orderDesc(ChatsDAO.Properties.TimeStamp)
     .where(ChatsDAO.Properties.UserId.eq(user_id))
     .where(new WhereCondition.StringCondition("1 GROUP BY chat_id"))
     .list();

but still, the code above gives me a list which contains the first message of each chat, instead of the recent message.

is there any way to make a raw query using greenDAO and select what I want?

Muhammad Awais
  • 448
  • 5
  • 17
Elior
  • 3,178
  • 6
  • 37
  • 67

1 Answers1

0

You can make a raw query using GreenDAO as follow. Also, check the link

 Query<User> query = userDao.queryBuilder().where(
 new StringCondition("_ID IN " +
 "(SELECT USER_ID FROM USER_MESSAGE WHERE READ_FLAG = 0)")
 ).build();
Muhammad Awais
  • 448
  • 5
  • 17
  • thanks for the suggestion. tried. didn't help. it keeps crashing. i'm trying to do something like in this link using greendao https://stackoverflow.com/questions/6572110/order-by-date-and-time-before-group-by-name-in-mysql – Elior Oct 22 '19 at 08:14
  • `.where(new WhereCondition.StringCondition("1 GROUP BY chat_id"))` Why you are using this in your query ? – Muhammad Awais Oct 22 '19 at 08:52
  • `.where(new WhereCondition.StringCondition("1 GROUP BY chat_id"))` try your query without using this .... – Muhammad Awais Oct 22 '19 at 08:55
  • 1
    I want to group all the messages by their chat_id. I saw that was the syntax for grouping in greendao. but doesn't matter- I used raw query for what I needed - seem to be working – Elior Oct 22 '19 at 08:56