Access 2016 - Return 如果项目存在于 Table 中则为真,如果项目缺失则为假

Access 2016 - Return TRUE if Item exists in Table, False if Item is Missing

好的,我有两个 table。

1 列出了一系列课程及其详细信息。 1 列出了一堆课程及其相关联系。

我需要创建一个查询来列出课程名称,列出所有课程,然后指出该课程是在课程中 (TRUE) 还是不存在 (FALSE)。

这是我到目前为止所尝试过的方法。

Left Outer Join for courses table 因为我想要与课程表相比的所有课程

计算每门课程的课程 ID 数量 - 仅不起作用 RETURNS“1”个值;并且只显示课程中的课程

找出两个 Table 之间的差异 - 不起作用,仅显示没有课程的值。

尝试了 IIF(IS NULL) 计算 - 仅不起作用 RETURNS 'TRUE' 值;并且只显示课程中的课程。

这应该很容易。有人可以帮我创建查询。基本上,我需要显示 BOTH table 中的所有值,然后显示与课程相关的值为 NULL 的位置。

Table 1:

COURSE ID    COURSE NAME
1              ENGLISH
2              FRENCH
3              DRAWING
4              SKETCHING

Table 2

Curriculum ID          Curriculum NameID        Course ID
1                      Senior   (actually #)       1
2                      Senior                      3
3                      Junior                      1
4                      Junior                      2
5                      Junior                      3

结果

Curriculum Name          Course Name             In Curriculum
Senior                     English                True
Senior                     French                 False
Senior                     Drawing                True
Senior                     Sketching              False
Junior                     English                True
Junior                     French                 True
Junior                     Drawing                True
Junior                     Sketching              False

TJ

由于您基本上在课程与课程表之间存在 many-to-many 关系(一门课程可能出现在多个课程表中,而一个课程表可能包含多个课程表),我建议按以下方式构建您的数据:

Table:课程

+-------+-----------+
| Co_ID |  Co_Desc  |
+-------+-----------+
|     1 | English   |
|     2 | French    |
|     3 | Drawing   |
|     4 | Sketching |
+-------+-----------+

Table:课程

+-------+---------+
| Cu_ID | Cu_Desc |
+-------+---------+
|     1 | Junior  |
|     2 | Senior  |
+-------+---------+

路口 Table:Curriculum_Courses

+----------+----------+
| CC_Cu_ID | CC_Co_ID |
+----------+----------+
|        1 |        1 |
|        1 |        2 |
|        1 |        3 |
|        2 |        1 |
|        2 |        3 |
+----------+----------+

然后,您的查询相对容易构建,因为您可以按以下方式使用 cross-join 和 left-join 的组合:

select 
    t.cu_desc as [Curriculum Name], 
    t.co_desc as [Course Name], 
    not cc_cu_id is null as [In Curriculum]
from
    (select * from curriculums, courses) t left join curriculum_courses u on
    t.cu_id = u.cc_cu_id and
    t.co_id = u.cc_co_id
order by
    t.cu_id, t.co_id