I recently build a thread based messaging system with the help from this post Thread Messaging
The 3 tables that involves are
member(member_id, member_userunique, and so on..)
thread_participant(thread_id, member_id)
message(message_id, message_subject, message_body, member_id, thread_id, and so on)
Within the participant table i have the following records
thread_id | member_id
1---------| 959
1---------| 1
1---------| 6
2---------| 959
2---------| 6
3---------| 959
3---------| 1
3---------| 6
And in the message table i have the following record (simplified version)
message_id | member_id | thread_id
1 ---------| 959 ------| 1
2 ---------| 959 ------| 2
3 ---------| 959 ------| 1
4 ---------| 959 ------| 1
5 ---------| 959 ------| 2
6 ---------| 959 ------| 3
7 ---------| 959 ------| 1
Hence we know that member id 959 and 1 are engage in 2 different threads, namely 3 (with message[s] id: 6) and 1 (with message[s] id: 1, 3 ,4 ,7).
However i am stuck with a particular query to get a particular thread based on two users.
So far this is the query that i have modified
SELECT m.message_id
FROM thread_participant AS participant
INNER JOIN message AS m ON participant.thread_id = m.thread_id
WHERE participant.member_id = 1 OR participant.member_id = 959
GROUP BY participant.thread_id
The query above is the short version of this query
SELECT message.message_id,
message.message_title, message.message_body,
message.message_sent_date,
user.member_userunique
FROM message AS message
INNER JOIN member AS user ON message.message_author = user.memberid
WHERE
(
message.message_id IN
(
SELECT m.message_id
FROM thread_participant AS participant INNER JOIN message AS m
ON participant.thread_id = m.thread_id
WHERE (participant.member_id IN (1,959))
GROUP BY participant.thread_id
)
)
AND message.message_title != 'UNDEFINED'
ORDER BY message.message_sent_date DESC
Note: The last query i am using the IN clause instead of the OR clause. There are more tables being used such as the Thread table which contains thread_id and creation_date.
Could anyone help me with this? Thx