3

我是SQL新手,请帮我解决问题。

我有一个用于站点私人消息的表,这是我在引擎之间迁移后得到的(phpbb 到 drupal)

可以说,它有 3 列:

  • mid - 用于消息 ID
  • 线程- 用于线程 ID
  • 收件人- 用户 ID,谁将收到消息。

每条消息都显示在收件人和作者的邮箱中,所以每条消息我都有 2 个字符串。

问题是消息的线程ID 不正确(基本上,它们等于mid),因此 2 个用户之间的对应关系显示为一百个单独的线程。

例如:

user 100 wrote message to user 101 
user 101 replied       to user 100
user 102 wrote message to user 100

该表如下所示:

 ________________________________
| 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       |
|________|___________|___________|

我的目标是为具有相同接收者对的每组对行提供相同的线程 ID:

 ________________________________
| 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       |
|________|___________|___________|

更新:

对于同一收件人对之间的所有当前对话,线程 ID 必须相同。(不用担心进一步的对话,用户可以选择在写消息时打开新线程,这将生成新线程 id,或者在现有线程中回复,这将使用现有线程 id 标记消息。我想要的只是收集线程中每 2 个用户之间的现有消息吨数)。

我想像某种循环一样,它将搜索每个中间的所有收件人 ID,并在升序排序后将它们合并到数组中:

  • 中间 1: (100, 101)
  • 中间 2: (100, 101)
  • 中间 3: (100, 102)

然后为相同的数组提供相同的 id,这将是我想要的线程 id:

  • (100, 101) = 1
  • (100, 101) = 1
  • (100, 102) = 2

不确定,如果我的算法仅适用于 SQL 查询

4

2 回答 2

0

这种更正应该从您的应用程序中更好地处理,因为在那里处理逻辑比在这种功能样式的 Sql 中更容易。

这是完成它的一种方法(这假设所有相关(线程)对话一个接一个,正如您所确认的那样):

SELECT   mid, 
         thread, 
         recipient 
FROM     (  
          SELECT row, 
                 mid, 
                 recipient,
                 IF(row % 2 = 0, @evenThread := thread, NULL) AS tempThread,
                 IF(row % 2 = 1, @evenThread, thread) AS thread
          FROM   (
                  SELECT   @row := @row + 1 as row, 
                           mid, 
                           IF(@row = 1, @thread := thread, NULL) AS tempThread,                
                           IF(@row % 2 = 1, IF(@row = 1, @rec1 := recipient, @rec1 := @rec3), NULL) AS tempOddRow,
                           IF(@row % 2 = 1, @rec3 := recipient, recipient) AS recipient,         
                           IF(@row % 2 = 0 AND @row > 2, IF((recipient != @rec1 OR @rec3 != @rec2) AND (recipient != @rec2 OR @rec3 != @rec1), @isDifferent := true, @isDifferent := false), NULL) AS isDifferent,
                           IF(@row % 2 = 0, @rec2 := recipient, NULL) AS tempEvenRow,
                           IF(@row % 2 = 0, IF(@isDifferent, @thread := @thread + 1, @thread), @thread) AS thread
                  FROM     (SELECT @row := 0) AS r, 
                           (SELECT @thread := 0) as t,
                           (SELECT @isDifferent := false) as d,
                           (SELECT @rec1 := 0) AS r1,
                           (SELECT @rec2 := 0) AS r2, 
                           (SELECT @rec3 := 0) AS r3, 
                           messages --your table name
                  ORDER BY row DESC
                 ) AS temp, 
                 (SELECT @evenThread := 0) AS t
         ) AS corrected 
ORDER BY row

这只是选择查询。在这里测试它http://sqlfiddle.com/#!9/61247/2。您可以将其复制到临时表中,并首先确认所有数据是否正确,而不是更新原始表,这会使调试更加困难。类似于以下内容:

CREATE TABLE messages_new LIKE messages; --new table name given
INSERT INTO  messages_new (mid, thread, recipient)
SELECT       mid, 
             thread, 
             recipient 
FROM         (  
              SELECT row, 
                     mid, 
                     recipient,
                     IF(row % 2 = 0, @evenThread := thread, NULL) AS tempThread,
                     IF(row % 2 = 1, @evenThread, thread) AS thread
              FROM   (
                      SELECT   @row := @row + 1 as row, 
                               mid, 
                               IF(@row = 1, @thread := thread, NULL) AS tempThread,                
                               IF(@row % 2 = 1, IF(@row = 1, @rec1 := recipient, @rec1 := @rec3), NULL) AS tempOddRow,
                               IF(@row % 2 = 1, @rec3 := recipient, recipient) AS recipient,         
                               IF(@row % 2 = 0 AND @row > 2, IF((recipient != @rec1 OR @rec3 != @rec2) AND (recipient != @rec2 OR @rec3 != @rec1), @isDifferent := true, @isDifferent := false), NULL) AS isDifferent,
                               IF(@row % 2 = 0, @rec2 := recipient, NULL) AS tempEvenRow,
                               IF(@row % 2 = 0, IF(@isDifferent, @thread := @thread + 1, @thread), @thread) AS thread
                      FROM     (SELECT @row := 0) AS r, 
                               (SELECT @thread := 0) as t,
                               (SELECT @isDifferent := false) as d,
                               (SELECT @rec1 := 0) AS r1,
                               (SELECT @rec2 := 0) AS r2, 
                               (SELECT @rec3 := 0) AS r3, 
                               messages
                      ORDER BY row DESC
                     ) AS temp, 
                     (SELECT @evenThread := 0) AS t
             ) AS corrected 
ORDER BY     row;
DROP TABLE   messages;
ALTER TABLE  messages_new RENAME TO messages; --back to old name
于 2013-03-23T20:23:22.830 回答
0

我在这里把它放到一个 SQL Fiddle 中:http ://sqlfiddle.com/#!2/83bdc/25

假设你的表名是messages我会做的:

Select messages.*, FirstThread 
From messages

Inner Join

(
-- Find a pair of dudes for each message and show the earliest thread of each
Select ThreadsForPairs.*, FirstThread From

(

Select mid
, Min(recipient) AS FirstDude
, Max(recipient) AS SecondDude
, thread

From messages

Group By mid, thread

) ThreadsForPairs

Left Join

(
-- Find the earliest thread for every pair of dudes
Select FirstDude, SecondDude, Min(thread) AS FirstThread

From

-- For every message, get the dudes involved
(
Select mid
, Min(recipient) AS FirstDude
, Max(recipient) AS SecondDude
, thread

From messages

Group By mid, thread
) PairsForMessages

Group By FirstDude, SecondDude

) FirstThreadForPairs

ON ThreadsForPairs.FirstDude = FirstThreadForPairs.FirstDude
AND ThreadsForPairs.SecondDude = FirstThreadForPairs.SecondDude

) FirstThreadForEveryMessage

On messages.mid = FirstThreadForEveryMessage.mid

正如您将看到的,我与您的输出不完全匹配,因为消息 ID 3 将具有线程 3 而不是 2,但我认为这是相同的想法......

于 2013-03-23T22:52:09.730 回答