从多个多个关系中选择

时间:2015-01-01 08:19:09

标签: sql asp.net sql-server sql-server-2008

我有这个database,我正在尝试选择员工可以采取的所有程序(课程),以及基于计划规范的类型和子类型,即员工工作和职位以及部门和单位和部分 所以我试图为拥有PROGRAM SPECIFICATION编号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

但是它会像这样返回结果

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行中,它为不同的程序类型返回相同的程序,因为它知道所需结果只是具有ProgramTypeName业务意识和ProgramSubTypeName常规的结果但是 因为ProgramTypeProgramSubType有多对多关系,所以ProgramType所有ProgramSubType知道需要与之关联的程序规范编号为7 {已选中)ProgramTypeName商家意识和ProgramSubTypeName一般

和最后一行14中的相同内容但是这里的ProgramType而不是Programs,而ProgramSubTypeProgramID具有多对多的关系,因此它返回结果具有相同ProgramSubTypeID且具有不同ProgramID的所有ProgramName都知道程序规范编号7的所需结果只是具有ProgramSubTypeID 116和{{1}}的程序41 所以,请帮助我如何做到这一点,并提前多多感谢。

2 个答案:

答案 0 :(得分:0)

因为你有两个多对多关系 - 表ProgramSubTypesAndProgram和ProgramTypeAndSubType你必须改变你的数据库模式以获得单个结果,例如。将精确类型添加到程序关系表ProgramSubTypesAndProgram或使Type-SubType关系只有一对多。

答案 1 :(得分:0)

我将我的数据库架构更改为此db schema,然后使用此两个查询 查看所有程序和详细信息并查看可用于定位的所有程序

查看所有程序及其详细信息:

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

它给出了想要的结果