删除重复记录

dev_forge
cxt 5 years ago
parent f8cf185ce1
commit 4633bbbb31

@ -1,5 +1,12 @@
class MigrateExerciseSingleQuestionScore < ActiveRecord::Migration[5.2]
def change
# 删除判断、单选题中生成多条记录但exercise_choice_id不同的数据
sql = %Q(delete from exercise_answers where (exercise_question_id, user_id) in
(select * from (select exercise_question_id, user_id from exercise_answers join exercise_questions eq on exercise_question_id = eq.id where eq.question_type in (0, 2) group by exercise_question_id, user_id, exercise_choice_id having count(*) > 1) a)
and id not in (select * from (select min(exercise_answers.id) from exercise_answers join exercise_questions eq on exercise_question_id = eq.id where eq.question_type in (0, 2) group by exercise_question_id, user_id having count(*) > 1 order by exercise_answers.id) b))
ActiveRecord::Base.connection.execute sql
# 更新成绩
exercise_answers = ExerciseAnswer.joins(:exercise_question).where("exercise_answers.created_at > '2019-11-01 00:00:00'").where(score: -1, exercise_questions: {question_type: [0, 2]})
exercise_answers.includes(:exercise_choice, exercise_question: :exercise_standard_answers).find_each do |answer|

@ -0,0 +1,10 @@
class AddTempMigration < ActiveRecord::Migration[5.2]
def change
# 删除判断、单选题中生成多条记录但exercise_choice_id不同的数据
sql = %Q(delete from exercise_answers where (exercise_question_id, user_id) in
(select * from (select exercise_question_id, user_id from exercise_answers join exercise_questions eq on exercise_question_id = eq.id where eq.question_type in (0, 2) group by exercise_question_id, user_id, exercise_choice_id having count(*) > 1) a)
and id not in (select * from (select min(exercise_answers.id) from exercise_answers join exercise_questions eq on exercise_question_id = eq.id where eq.question_type in (0, 2) group by exercise_question_id, user_id having count(*) > 1 order by exercise_answers.id) b))
ActiveRecord::Base.connection.execute sql
end
end
Loading…
Cancel
Save