INSERT IGNORE INTO
資料限制只能一筆,不能重複。但此方法若其中某一欄位的值不同,就不能視為相同。例如:時間變數 timecreated, timemodified
不一樣,無法視為相同,故還是會新增一筆。
/usr/bin/mysql -uroot -prootpw << EOF use moodle23; set @tem1=(select id from mdl_enrol where courseid="$id" and enrol="manual"); set @tem2=(select id from mdl_user where username="$username"); /*select @tem1;select @tem2;*/ insert INGORE into mdl_user_enrolments (enrolid,userid,timestart,timeend,modifierid,timecreated,timemodified) values (@tem1,@tem2,"$timestart","","2","$timecreated","$timemodified"); set @cont=(select id from mdl_context where instanceid="$id" and contextlevel='50'); insert INGORE into mdl_role_assignments (roleid,contextid,userid,timemodified,modifierid) values ('5',@cont,@tem2,"$timemodified","2"); EOF
WHERE @tem1 IS NOT NULL AND @tem2 IS NOT NULL;
WHERE NOT EXISTS (SELECT id FROM mdl_user_enrolments WHERE enrolid=@tem1 and userid=@tem2) LIMIT 1;
/usr/bin/mysql -uroot -prootpw << EOF use moodle23; set @tem1=(select id from mdl_enrol where courseid="$id" and enrol="manual"); set @tem2=(select id from mdl_user where username="$username"); /*select @tem1;select @tem2;*/ insert into mdl_user_enrolments (enrolid,userid,timestart,timeend,modifierid,timecreated,timemodified) select @tem1,@tem2,"$timestart","","2","$timecreated","$timemodified" from DUAL WHERE @tem1 IS NOT NULL AND @tem2 IS NOT NULL AND NOT EXISTS (SELECT id FROM mdl_user_enrolments WHERE enrolid=@tem1 and userid=@tem2) LIMIT 1; set @cont=(select id from mdl_context where instanceid="$id" and contextlevel='50'); insert into mdl_role_assignments (roleid,contextid,userid,timemodified,modifierid) select '5',@cont,@tem2,"$timemodified","2" FROM DUAL WHERE @cont IS NOT NULL AND @tem2 IS NOT NULL AND NOT EXISTS (SELECT id FROM mdl_role_assignments WHERE contextid=@cont and userid=@tem2) LIMIT 1; EOF