如何从笛卡尔乘积查询中追加 table 中尚不存在的记录?
How can I append records that don't already exist in a table from a cartesian product query?
我当前的查询 returns 来自 2 table 的所有可能结果,并将它们插入连接点 table。
我当前的查询如下所示:
INSERT INTO tblJunction_Courses_Software ( CourseID, SoftwareID )
SELECT tblCourses.CourseID, tblSoftware.SoftwareID
FROM tblSoftware, tblCourses
WHERE (((tblSoftware.Exclude)=No));
如何添加附加子句以确保每次查询时都不会附加重复数据运行?
我想你可以用 not exists
子句做你想做的事:
INSERT INTO tblJunction_Courses_Software ( CourseID, SoftwareID )
SELECT c.CourseID, s.SoftwareID
FROM tblSoftware as s CROSS JOIN tblCourses as c
WHERE s.Exclude = No AND
NOT EXISTS (SELECT 1
FROM tblJunction_Courses_Software as cs
WHERE cs.CourseId = c.CourseId AND
cs.SoftwareId = s.SOftwareId
);
我当前的查询 returns 来自 2 table 的所有可能结果,并将它们插入连接点 table。
我当前的查询如下所示:
INSERT INTO tblJunction_Courses_Software ( CourseID, SoftwareID )
SELECT tblCourses.CourseID, tblSoftware.SoftwareID
FROM tblSoftware, tblCourses
WHERE (((tblSoftware.Exclude)=No));
如何添加附加子句以确保每次查询时都不会附加重复数据运行?
我想你可以用 not exists
子句做你想做的事:
INSERT INTO tblJunction_Courses_Software ( CourseID, SoftwareID )
SELECT c.CourseID, s.SoftwareID
FROM tblSoftware as s CROSS JOIN tblCourses as c
WHERE s.Exclude = No AND
NOT EXISTS (SELECT 1
FROM tblJunction_Courses_Software as cs
WHERE cs.CourseId = c.CourseId AND
cs.SoftwareId = s.SOftwareId
);