0

我有一个表格,其中包含一个测试库的每两个问题的相似性。 在此处输入图像描述

这意味着 question_id与 question_id6有 84% 的相似性10。并且有 12 个类似 question_id 的问题6

我只是最相关的问题,或前 7 个相关问题。

我看过Mysql delete order by 并尝试过:

DELETE FROM     exam_relatedquestion
WHERE
    `exam_relatedquestion`.id IN (
        SELECT
            `exam_relatedquestion`.id
        FROM
            (
                SELECT  `exam_relatedquestion`.id
                FROM    `exam_relatedquestion`
                GROUP BY
                    `exam_relatedquestion`.from_question_id_id
                ORDER BY
                    `exam_relatedquestion`.similarity DESC
                LIMIT 7
            ) a
    )

但错误信息是:

[Err] 1055 - SELECT 列表的表达式 #1 不在 GROUP BY 子句中,并且包含在功能上不依赖于 GROUP BY 子句中的列的非聚合列“den.exam_relatedquestion.id”;这与 sql_mode=only_full_group_by 不兼容

如何删除与问题无关的前 7 个行?

4

1 回答 1

1

那是行不通的。无论如何,您的伪代码都不正确,因为排序方向错误。

在任何情况下,您都可以使用变量来枚举问题,然后使用join

delete erq
    from exam_relatedquestion erq join
         (select erq2.*,
                 (@rn := if(@q = erq2.from_question_id_id, @rn + 1,
                            if(@q := erq2.from_question_id_id, 1, 1)
                           )
                 ) as seqnum
          from exam_relatedquestion erq2 cross join
               (select @rn := 0, @q := -1) params
          order by erq2.from_question_id_id, score desc
         ) erq2
         on erq2.id = erq.id
    where rn > 7;
于 2017-05-17T01:55:46.580 回答