postgresql"插入......关于冲突...更新"如何指定要更新的行

时间:2015-12-17 20:48:01

标签: postgresql merge upsert

我不知道如何做这项工作,希望你能帮助我

我有一张桌子" student_to_course_term"包含列" course_term_id"," student_id"," student_mc_worksheet_id"和" id"

我想要实现的是将course_term_id = 19的所有行复制到course_term_id = 23的新行。如果行已存在,请更新" student_mc_worksheet_id"。

到目前为止,这是我的代码,但我不确定如何在#34;冲突后更新set"并使用course_term_id = 19和course_term_id = 23来访问该行。部分。任何帮助表示赞赏!


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) ;

0 个答案:

没有答案