Home How can I can all unique user whom I send message and receive message

# How can I can all unique user whom I send message and receive message

user8119
1#
user8119 Published in September 20, 2018, 12:43 pm

I have table in which all conversation is stored like this

Suppose I have a table like this (fromUser and toUser is fk from user table)

-----------------------------------------
Id.  |  FromUser  |  toUser  | message  |
|            |          |          |
1    |     1      |    2     |  Hi      |
2    |     2      |    1     |  hello   |
3    |     3      |    1     |  hi      |
4    |     1      |    4     |  hello   |
-----------------------------------------


Desired output for distinct row for user 1 orderBy last updated date

-----------------------------------------
Id.  |  FromUser  |  toUser  | message  |
|            |          |          |
2    |     2      |    1     |  hello   |
3    |     3      |    1     |  hi      |
4    |     1      |    4     |  hello   |
-----------------------------------------


Desired output for distinct row for user 2 orderBy last date

-----------------------------------------
Id.  |  FromUser  |  toUser  | message  |
|            |          |          |
2    |     2      |    1     |  hello   |
-----------------------------------------


Desired output for distinct row for user 3 orderBy last date

-----------------------------------------
Id.  |  FromUser  |  toUser  | message  |
|            |          |          |
3    |     3      |    1     |  hi      |
-----------------------------------------


As well for other users

• Where is your last date field in schema? – D-Shih Feb 13 at 2:08
• let assume table is in order by updated date.. last entry added having updated time and I want to consider last entry as row – Abhishek Agarwal Feb 13 at 5:15
• Let's assume you are somewhere using the JPA API, then you would know that CLASSES are of importance not TABLES, so post the classes instead of the tables. If instead you are simply wanting SQL then remove the JPA tag – user8558216 Feb 13 at 8:28