INSERT INTO base_class_course (class_id, course_id, textbook_id, create_date, delete_mark, enabled_mark) VALUES (#{classId}, #{courseId}, #{textbookId},now(),0,1) ON DUPLICATE KEY UPDATE class_id = #{classId}, course_id = #{courseId}, textbook_id = #{textbookId} UPDATE base_class_course SET delete_mark = 1 WHERE class_id = #{classId} AND course_id = #{courseId} AND textbook_id = #{textbookId} UPDATE base_class_course SET delete_mark = 1 WHERE class_id = #{newClassId} AND (course_id IS NULL OR textbook_id IS NULL) INSERT INTO base_class_course (class_id, course_id, textbook_id, create_date, delete_mark, enabled_mark) SELECT #{newClassId}, t.course_id, t.textbook_id, NOW(), 0, 1 FROM (SELECT course_id, textbook_id FROM base_class_course WHERE class_id = #{sourceClassId} AND delete_mark = 0 AND EXISTS ( SELECT 1 FROM textbook tb WHERE tb.id = textbook_id AND tb.base_semester_id = #{semester} ) GROUP BY course_id, textbook_id ) t ON DUPLICATE KEY UPDATE delete_mark = 0, enabled_mark = 1, create_date = NOW()