MySQL DELETE行ORDER BY COUNT DESC

时间:2017-05-17 01:47:07

标签: mysql group-by mysql-error-1055

我有一张表格,其中包含测试银行每两个问题的相似性。 enter image description here

这意味着,question_id 6与question_id 10的相似度为84%。 而且有12个类似问题的问题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子句中   并包含非聚合列'den.exam_relatedquestion.id'   在功能上不依赖于GROUP BY子句中的列;这是   与sql_mode = only_full_group_by

不兼容

如何删除不属于问题前7位的任何行?

1 个答案:

答案 0 :(得分: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;