从多个多对多关系中选择

Selecting from multiple many to many relationshipes

嗨,我有这个 database,我正在尝试 select 员工可以参加的所有项目(课程)以及基于项目规范的类型和子类型,即员工工作and POSITION and DEPARTMENT and UNIT 和 SECTION 所以我正在尝试 select 为程序规范编号为 7 的员工编写的所有程序 但是当我尝试这样做时:

SELECT * FROM [dbo].[ProgramSpecification]
              INNER JOIN [dbo].[ProgramsSpecificationDetails] ON [dbo].[ProgramSpecification].PROGRAM_SPECIFCATION_ID = [dbo].[ProgramsSpecificationDetails].PROGRAM_SPECIFCATION_ID
              INNER JOIN [dbo].[Programs] ON [dbo].[Programs].ProgramID=[dbo].[ProgramsSpecificationDetails].ProgramID 
              INNER JOIN [dbo].[ProgramSubTypesAndProgram] ON [dbo].[Programs].ProgramID = [dbo].[ProgramSubTypesAndProgram].ProgramID
              INNER JOIN [dbo].[ProgramSubTypes] ON [dbo].[ProgramSubTypes].ProgramSubTypeID =  [dbo].[ProgramSubTypesAndProgram].ProgramSubTypeID
              INNER JOIN [dbo].[ProgramTypeAndSubType] ON [dbo].[ProgramSubTypes].ProgramSubTypeID = [dbo].[ProgramTypeAndSubType].ProgramSubTypeID
              INNER JOIN [dbo].[ProgramsTypes] ON [dbo].[ProgramsTypes].ProgeamTypeID = [dbo].[ProgramTypeAndSubType].ProgeamTypeID

预期结果是:

PROGRAM_SPECIFCATION_ID POSITION_ID POSITION    JOB_ID  JOB DEPARTMENT_ID   DEPARTMENT_NAME UNIT_ID UNIT_NAME   SECTION_ID  SECTION_NAME    ProgramID   PROGRAM_SPECIFCATION_ID ProgramID   ProgramName ProgramSubTypeID    ProgramID   ProgramSubTypeID    ProgramSubTypeName  ProgeamTypeID   ProgramSubTypeID    ProgeamTypeID   ProgramTypeName NominationTypeID
7   12424   Account Payable Accountant  48  Accountant  587 Finance 650 Accounting  582 Account Payables    1   7   1   Corporate Culture   1   1   1   General 1   1   1   Business-Awareness  1
7   12424   Account Payable Accountant  48  Accountant  587 Finance 650 Accounting  582 Account Payables    2   7   2   Business Etiquette  1   2   1   General 1   1   1   Business-Awareness  1
7   12424   Account Payable Accountant  48  Accountant  587 Finance 650 Accounting  582 Account Payables    3   7   3   Technical Awareness 2   3   2   TC  1   2   1   Business-Awareness  1
7   12424   Account Payable Accountant  48  Accountant  587 Finance 650 Accounting  582 Account Payables    4   7   4   Information Security For non Technical People   3   4   3   IS  1   3   1   Business-Awareness  1
7   12424   Account Payable Accountant  48  Accountant  587 Finance 650 Accounting  582 Account Payables    18  7   18  Supply Chain Management 11  18  11  Procurement 2   11  2   Business-Specialized    1
7   12424   Account Payable Accountant  48  Accountant  587 Finance 650 Accounting  582 Account Payables    33  7   33  Hyperion Financial Management   13  33  13  Finance 2   13  2   Business-Specialized    1
7   12424   Account Payable Accountant  48  Accountant  587 Finance 650 Accounting  582 Account Payables    35  7   35  Oracle Financial Applications Overview  13  35  13  Finance 2   13  2   Business-Specialized    1
7   12424   Account Payable Accountant  48  Accountant  587 Finance 650 Accounting  582 Account Payables    36  7   36  IFRS    13  36  13  Finance 2   13  2   Business-Specialized    1
7   12424   Account Payable Accountant  48  Accountant  587 Finance 650 Accounting  582 Account Payables    116 7   116 The 7 Habits    41  116 41  Achievement Orientation 4   41  4   CompetencyBased 1

而是 return 这样的结果

PROGRAM_SPECIFCATION_ID POSITION_ID POSITION    JOB_ID  JOB DEPARTMENT_ID   DEPARTMENT_NAME UNIT_ID UNIT_NAME   SECTION_ID  SECTION_NAME    ProgramID   PROGRAM_SPECIFCATION_ID ProgramID   ProgramName ProgramSubTypeID    ProgramID   ProgramSubTypeID    ProgramSubTypeName  ProgeamTypeID   ProgramSubTypeID    ProgeamTypeID   ProgramTypeName NominationTypeID
7   12424   Account Payable Accountant  48  Accountant  587 Finance 650 Accounting  582 Account Payables    1   7   1   Corporate Culture   1   1   1   General 1   1   1   Business-Awareness  1
7   12424   Account Payable Accountant  48  Accountant  587 Finance 650 Accounting  582 Account Payables    1   7   1   Corporate Culture   1   1   1   General 3   1   3   Business-Technical  1
7   12424   Account Payable Accountant  48  Accountant  587 Finance 650 Accounting  582 Account Payables    1   7   1   Corporate Culture   1   1   1   General 5   1   5    Leadership 2
7   12424   Account Payable Accountant  48  Accountant  587 Finance 650 Accounting  582 Account Payables    2   7   2   Business Etiquette  1   2   1   General 1   1   1   Business-Awareness  1
7   12424   Account Payable Accountant  48  Accountant  587 Finance 650 Accounting  582 Account Payables    2   7   2   Business Etiquette  1   2   1   General 3   1   3   Business-Technical  1
7   12424   Account Payable Accountant  48  Accountant  587 Finance 650 Accounting  582 Account Payables    2   7   2   Business Etiquette  1   2   1   General 5   1   5    Leadership 2
7   12424   Account Payable Accountant  48  Accountant  587 Finance 650 Accounting  582 Account Payables    3   7   3   Technical Awareness 2   3   2   TC  1   2   1   Business-Awareness  1
7   12424   Account Payable Accountant  48  Accountant  587 Finance 650 Accounting  582 Account Payables    4   7   4   Information Security For non Technical People   3   4   3   IS  1   3   1   Business-Awareness  1
7   12424   Account Payable Accountant  48  Accountant  587 Finance 650 Accounting  582 Account Payables    18  7   18  Supply Chain Management 11  18  11  Procurement 2   11  2   Business-Specialized    1
7   12424   Account Payable Accountant  48  Accountant  587 Finance 650 Accounting  582 Account Payables    33  7   33  Hyperion Financial Management   13  33  13  Finance 2   13  2   Business-Specialized    1
7   12424   Account Payable Accountant  48  Accountant  587 Finance 650 Accounting  582 Account Payables    35  7   35  Oracle Financial Applications Overview  13  35  13  Finance 2   13  2   Business-Specialized    1
7   12424   Account Payable Accountant  48  Accountant  587 Finance 650 Accounting  582 Account Payables    36  7   36  IFRS    13  36  13  Finance 2   13  2   Business-Specialized    1
7   12424   Account Payable Accountant  48  Accountant  587 Finance 650 Accounting  582 Account Payables    116 7   116 The 7 Habits    41  116 41  Achievement Orientation 4   41  4   CompetencyBased 1
7   12424   Account Payable Accountant  48  Accountant  587 Finance 650 Accounting  582 Account Payables    116 7   116 The 7 Habits    42  116 42  Adaptability    4   42  4   CompetencyBased 1

在第 2 3 5 6 行它 return 不同程序类型的相同程序知道想要的结果只是具有 ProgramTypeName 业务意识和 ProgramSubTypeName 的结果一般但 因为 ProgramTypeProgramSubType 有多对多的关系 它 return 所有 ProgramTypeProgramSubType 一般知道程序规范编号 7 它需要link selected) ProgramTypeName 业务意识和 ProgramSubTypeName 一般

和最后一行 14 中的相同内容,但这里的 ProgramType 两个表是 ProgramsProgramSubType,它们具有多对多关系,因此它 return 在结果中所有具有相同 ProgramID 和不同 ProgramSubTypeID 的 ProgramName 知道 PROGRAM SPECIFICATION 编号 7 的想要的结果只是具有 ProgramID 116 和 [=25 的程序=] 41 所以请帮助我如何做到这一点,非常感谢。

因为您有两个多对多关系 - tables ProgramSubTypesAndProgram 和 ProgramTypeAndSubType,您必须更改您的数据库架构才能获得单一结果,例如。将精确类型添加到程序关系 table ProgramSubTypesAndProgram 或使类型-子类型关系仅一对多。

我将我的数据库模式更改为这样 然后使用这个拖车查询来 查看所有程序和那里的详细信息并查看可用于某个职位的所有程序

查看所有程序并查看详细信息:

SELECT *
FROM [dbo].[Programs]
INNER JOIN [dbo].[ProgramDetails] ON [dbo].[Programs].ProgramID =  [dbo].[ProgramDetails].ProgramID
INNER JOIN [dbo].[ProgramSubTypes] ON [dbo].[ProgramSubTypes].ProgramSubTypeID = [dbo].[ProgramDetails].ProgramSubTypeID
INNER JOIN [dbo].[ProgramsTypes] ON [dbo].[ProgramsTypes].ProgramTypeID = [dbo].[ProgramDetails].ProgramTypeID

查看某个职位可用的所有程序:

SELECT * 
FROM [dbo].[Positions] 
INNER JOIN [dbo].[ProgramsSpecificationDetails] ON [dbo].[Positions].PositionID =  [dbo].[ProgramsSpecificationDetails].PositionID
INNER JOIN [dbo].[ProgramDetails] ON [dbo].[ProgramDetails].ProgramDetailsID = [dbo].[ProgramsSpecificationDetails].ProgramDetailsID 
INNER JOIN [dbo].[Programs] ON [dbo].[Programs].ProgramID =  [dbo].[ProgramDetails].ProgramID
INNER JOIN [dbo].[ProgramSubTypes] ON [dbo].[ProgramSubTypes].ProgramSubTypeID = [dbo].[ProgramDetails].ProgramSubTypeID
INNER JOIN [dbo].[ProgramsTypes] ON [dbo].[ProgramsTypes].ProgramTypeID = [dbo].[ProgramDetails].ProgramTypeID

它给出了想要的结果