I am trying to merge data from two tables. Following is the structure:
Users
userID (Primary Key),
firstName,
lastName.
Sample data:
[1, John, Smith]
[2, Steve, Lan]
[3, Matt, Smith]
Message
messageID (Primary Key),
sender_userID,
receiver_userID,
messageBody.
Sample data:
[1, 1, 1, Hello]
[2, 1, 2, Second Message]
[3, 2, 1, ThirdMessage]
[4, 2, 1, FourthMessage]
[5, 3, 3, LastMessage]
Now, I need to display all messages posted/received by a user ID and his colleagues.
In above sample data, userID 1 is a colleague of userID 2. So, if I query for userID = 1, then I should get message ID 1, 2, 3 [Since Message.sender_userID IN (1,2)] and 4 [Since Message.receiver_userID= 1]. MessageID = 5 should not appear because 3 is not a colleague of any one. Along with this, it should print firstName and lastName for both sender_userID, receiver_userID.
Result with unique messageID should be:
Message.messageID,
Users.userID (Sender),
Users.firstName (Sender),
Users.lastName (Sender),
Users.userID (Receiver),
Users.firstName (Receiver),
Users.lastName (Receiver),
Message.messageBody
I know the query can done using JOIN, but I am new to SQL and some how not able to get the desired output.
Any help would be appreciated.
Thanks