1

I have no idea how to make this work, hope you can help me

I have a table "student_to_course_term" containing the columns "course_term_id", "student_id", "student_mc_worksheet_id" and "id"

What I want to achieve is to copy all rows with course_term_id=19 to new rows with course_term_id=23. If row already exists, update the "student_mc_worksheet_id".

This is my code so far, but I'm not sure how to access the row with course_term_id=19 and course_term_id=23 after the "on conflict do update set" part. Any help appreciated!


INSERT INTO public.student_to_course_term
(course_term_id, student_id, student_mc_worksheet_id)
SELECT 23, student_id, student_mc_worksheet_id
FROM public.student_to_course_term
WHERE course_term_id = 19
ON CONFLICT ON CONSTRAINT student_to_course_term_student_id_course_term_id_key
DO UPDATE
SET student_mc_worksheet_id ( =>from the student_mc_worksheet_id=23 row) = student_mc_worksheet_id ( => from the student_mc_worksheet_id=19 row) ;

1
  • You always update the "conflicting" row, and you can access the values that would have been inserted using using the alias excluded. So just use set student_mc_worksheet_id = excluded.student_mc_worksheet_id
    – user330315
    Dec 17, 2015 at 20:51

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.