엄청난 양의 (중복 된) 행을 생성하는 불평등에 대해 교차 결합을 수행하고 있습니다.
대신 동등성을 위해 LEFT JOIN
을 수행하고 null
개의 행을 걸러 내야합니다.
나는 그것으로 다시 것 :
여기
student_id
INSERT INTO table2
(`student_id`, `studentname`, `Regno`, `class`, `date`, `session`
, `status`, `teacher_id`)
SELECT t1.* FROM table1 t1
LEFT JOIN table2 t2 ON (t1.student_id = t2.student_id)
WHERE t1.`date` BETWEEN '2011-01-01' AND '2011-05-19'
AND t1.`class` = 'AAA'
AND t2.student_id IS NULL
은 T1과 T2 모두에 대한 기본 키입니다. 약동학은 (student_id + teacher_id) 인 경우 다음 쿼리가된다 : 그것이 작동하는 방법
INSERT INTO table2
(`student_id`, `studentname`, `Regno`, `class`, `date`, `session`
, `status`, `teacher_id`)
SELECT t1.* FROM table1 t1
LEFT JOIN table2 t2 ON (t1.student_id = t2.student_id
AND t1.teacher_id = t2.teacher_id)
WHERE t1.`date` BETWEEN '2011-01-01' AND '2011-05-19'
AND t1.`class` = 'AAA'
AND t2.student_id IS NULL /*<<-- this stays the same provided student_id is
<<-- defined as `NOT NULL` */
가 여기에 있습니다.
먼저 모든 행을 선택합니다. (t1.student_id = t2.student_id)
; 이것은 t1과 t2에서 일치하는 모든 행을 정렬합니다.
왼쪽 조인이므로 t1에는 있지만 t2에는없는 행은 t2 열에 null
값을 갖습니다.
t2.student_id IS NULL
의 행만 허용함으로써 t2에서 일치하는 행이없는 행을 t1에서 선택합니다.
table2에 기본 키가 있습니까? 중복을 어떻게 정의합니까? 모든 열은 동일하거나 첫 번째 ... 등입니까? – Jacob
나는'student_id'가 PK (또는 아마도 콤보 (student_id + teacher_id))가 될 것을 기대할 것입니다. – Johan