vendredi 28 novembre 2014

How can I insert rows into a table from a select query that brings duplicate entries?


I am using PostgreSQL.


I have a table with columns:



tutor_id
subject_id


I want to populate this table with the result of the following select:



select tutors.id, subject_id from courses_subjects
join courses on courses.id = courses_subjects.course_id
join tutors on tutors.id = courses.tutor_id;


The point is that the above select brings duplicate entries. This breaks the insert



insert into tutor_subjects (tutor_id, subject_id)
select tutors.id, subject_id
from ...;


because tutor_subjects does not allow duplicate entries. The correct data that I have to insert appear to be:



select distinct(tutors.id, subject_id) from ....;


But I cannot use this select in combination with insert.


Any help?





Aucun commentaire:

Enregistrer un commentaire