【问题标题】:How can I select the last edited version of the post?如何选择帖子的最后编辑版本?
【发布时间】:2017-01-03 19:16:09
【问题描述】:

我有一个像 SO 这样的问答网站。我还有一个表格,其中包含问题和答案及其编辑版本。这是我的表结构:

// QandA
+----+---------+---------------------------+---------+------+-----------+
| id |  title  |            body           | related | type | edited_id |
+----+---------+---------------------------+---------+------+-----------+
| 1  | title1  | question content          | NULL    | 0    | NULL      |
| 2  |         | answer content            | 1       | 1    | NULL      | 
| 3  | title2  | question content          | NULL    | 0    | NULL      |
| 4  |         | answer content            | 3       | 1    | NULL      |
| 5  |         | answer content            | 1       | 1    | NULL      |
| 6  |         | answer content (edited)   | NULL    | 1    | 2         |
| 7  | title3  | question content          | NULL    | 0    | NULL      |
| 8  | title1  | question content (edited) | NULL    | 0    | 1         |
| 9  |         | answer content            | 7       | 1    | NULL      |
| 10 | title1  | question content (edited) | NULL    | 0    | 1         |
| 11 | title3  | question content (edited) | NULL    | 0    | 7         |
+----+---------+---------------------------+---------+------+-----------+

栏目说明:

related专栏:

  • NULL 用于问题和问题/答案的编辑版本
  • {the id of its own question}求答案

type专栏:

  • 0 提问
  • 1求答案

edited_id专栏: (原帖id)

  • NULL 表示这是一个原始问题/答案
  • {any number} 表示它是问题/答案的编辑版本。

现在我需要一个查询来选择一个问题及其所有答案。注意到我需要选择它们的最后一个编辑版本(如果它们已被编辑)

Example1:我有这个值::id = 1,我想要这个输出:

+----+---------+---------------------------+---------+------+-----------+
| id |  title  |            body           | related | type | edited_id |
+----+---------+---------------------------+---------+------+-----------+
| 10 | title1  | question content (edited) | NULL    | 0    | 1         |
| 6  |         | answer content (edited)   | NULL    | 1    | 2         |
| 5  |         | answer content            | 1       | 1    | NULL      |
+----+---------+---------------------------+---------+------+-----------+

Example2:我有这个值::id = 3,我想要这个输出:

+----+---------+---------------------------+---------+------+-----------+
| id |  title  |            body           | related | type | edited_id |
+----+---------+---------------------------+---------+------+-----------+
| 3  | title2  | question content          | NULL    | 0    | NULL      |
| 4  |         | answer content            | 3       | 1    | NULL      |
+----+---------+---------------------------+---------+------+-----------+

Example2:我有这个值::id = 7,我想要这个输出:

// QandA
+----+---------+---------------------------+---------+------+-----------+
| id |  title  |            body           | related | type | edited_id |
+----+---------+---------------------------+---------+------+-----------+
| 11 | title3  | question content (edited) | NULL    | 0    | 7         |
| 9  |         | answer content            | 7       | 1    | NULL      |
+----+---------+---------------------------+---------+------+-----------+

这是我当前的查询:

SELECT *
FROM QandA
WHERE (id = :id AND type = 0) OR
      (related = :id AND type = 1)
ORDER BY type -- noted that the order of answers doesn't matter

如您所见,我的查询不支持编辑版本。无论如何,当该帖子有已编辑的行时,如何替换已编辑的帖子版本?

注意:请不要告诉我“不要将问题和答案放在同一张表中”,因为我知道。但是现在我需要解决上面的问题。

【问题讨论】:

  • 你真的需要一个timestamp 列。如果你有它会容易得多。
  • @1000111 我可以添加一个名为date_time 的新列(实际上我的真实表结构中有它) .. 但我认为它不会有用.在这种情况下,我猜它的行为与 id 列完全相同。
  • 我认为如果问题和答案是两个单独的表格会更容易,但无论如何......
  • id==6可以关联=1吗?
  • 使用多个查询。先得到你的问题,然后得到你的答案或每个答案。

标签: mysql sql database select


【解决方案1】:

您可以尝试以下查询:

SET @qid := 1;

SELECT 
QA.*
FROM QA 
INNER JOIN 
(
    SELECT 
    MAX(GREATEST(A.id, COALESCE(B.id, 0))) latest_id
    FROM QA A 
    LEFT JOIN QA B ON A.id = B.edited_id 
    WHERE @qid IN(A.id,A.related,A.edited_id)
    GROUP  BY A.type , IF(A.type = 1, A.id,0)
) AS t 
ON QA.id = t.latest_id

WORKING DEMO

【讨论】:

  • 只有一件事 .. 您查询甚至选择了一些行 when I set @qid the id of an answer .. 应该是这样 .. 它应该选择一个问题及其所有答案 (激光编辑的版本他们) 当我设置问题的 id 时。
  • 如果您传递问题 ID,则此答案应该有效。我真的不认为你也可以传递答案的 id。
  • 那么,当您提供答案的 id 时,预期的行为是什么? @MartinAJ
  • 不应选择任何行。
  • 然后您可以在 where 子句中添加一个额外的条件,例如 that
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-04-28
  • 1970-01-01
  • 2016-07-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多