I'm very new in SQL, please help me to solve the problem.
I have a table for site's private messages, that i got after migration between engines (phpbb to drupal)
Lets say, it has 3 columns:
- mid - for message id
- thread - for thread id
- recipient - user id, who will get the message.
Every message is displayed in recipient's and in autor's mailbox, so i have 2 strings for every message.
The problem is that the thread id's for the messages are incorrect (basically, they are equal to mid), so correspondence between 2 users displays as a hundred of seperate threads.
For example:
user 100 wrote message to user 101
user 101 replied to user 100
user 102 wrote message to user 100
The table looks like:
________________________________
| mid | thread | recipient |
| | | |
| 1 | 1 | 101 | ← message 1 in recipient's mailbox
| 1 | 1 | 100 | ← message 1 in author's mailbox
| 2 | 2 | 100 | ...
| 2 | 2 | 101 |
| 3 | 3 | 100 |
| 3 | 3 | 102 |
|________|___________|___________|
My goal is to give the same thread id to every group of pair lines with the same pair of recipients:
________________________________
| mid | thread | recipient |
| | | |
| 1 | 1 | 101 | }
| 1 | 1 | 100 | } Correspondence between 100 and 101 must
| 2 | 1 | 100 | } have the same thread id (1)
| 2 | 1 | 101 | }
| 3 | 2 | 100 |
| 3 | 2 | 102 |
|________|___________|___________|
UPDATE:
Thread id must be the same for all present conversations between the same recipient's pairs. (Don't worry about further conversations, users will have an option to open new thread when writing message, that will generate new thread id, or reply in existing thread, that will mark message with existing thread id. All i want is to gather tonnes of existing messages between each 2 users in threads).
I imagine that like some sort of cycle, that will search all the recipients id's for every mid and merge them in array after ascending sort:
- For mid 1: (100, 101)
- For mid 2: (100, 101)
- For mid 3: (100, 102)
Then give the same arrays same id that will be my desired thread id:
- (100, 101) = 1
- (100, 101) = 1
- (100, 102) = 2
Not sure, if my algorithm is possible with only SQL query