删除重复记录

dev_forge
cxt 5 years ago
parent 4633bbbb31
commit a4e0cd9f33

@ -2,7 +2,7 @@ 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)
(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 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

@ -2,7 +2,7 @@ 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)
(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 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

@ -0,0 +1,10 @@
class AddExerciseTempMigration < 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 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