![](/img/trans.png)
[英]Get all records from Table A and all entries that don't already exist from Table B
[英]How can I append records that don't already exist in a table from a cartesian product query?
我當前的查詢從2個表中返回所有可能的結果,並將它們插入聯結表中。
我當前的查詢如下所示:
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
);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.