Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 从多对多关系中选择_Sql_Asp.net_Sql Server_Sql Server 2008 - Fatal编程技术网

Sql 从多对多关系中选择

Sql 从多对多关系中选择,sql,asp.net,sql-server,sql-server-2008,Sql,Asp.net,Sql Server,Sql Server 2008,嗨,我有这个,我正在尝试选择员工可以参加的所有课程,这些课程类型和子类型基于课程规范,即员工的工作和职位、部门、单位和部门 因此,我试图为员工选择所有程序规范编号为7的程序 但当我试着这样做的时候: SELECT * FROM [dbo].[ProgramSpecification] INNER JOIN [dbo].[ProgramsSpecificationDetails] ON [dbo].[ProgramSpecification].PROGRAM_SPECI

嗨,我有这个,我正在尝试选择员工可以参加的所有课程,这些课程类型和子类型基于课程规范,即员工的工作和职位、部门、单位和部门 因此,我试图为员工选择所有程序规范编号为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 Business Aware和ProgramSubjectName General的结果,但是 由于ProgramType和ProgramSubType具有多对多关系,因此它返回所有具有ProgramSubType General的ProgramType,知道需要使用ProgramTypeName Business Aware和ProgramSubType Name General链接的程序规范编号7

在最后一行14中也是一样,但是这里不是ProgramType,而是两个表是程序和ProgramSubType,它们有多对多的关系,所以它在结果中返回所有程序名,这些程序名具有相同的ProgramID和不同的ProgramSubType,知道程序规范编号7的所需结果只是程序ID为116和程序子类型为41的程序
因此,请提供我如何做到这一点的帮助,并提前表示感谢。

因为您有两个多对多关系-表ProgramSubjects和Program以及ProgramTypes和Subtype您必须更改db模式才能获得单个结果,例如,将精确类型添加到程序关系表programsubitesandprogram或使类型子类型关系仅为一对多。

我将我的数据库模式更改为这样,然后使用此两个查询 查看所有程序及其详细信息,并查看可用于某个职位的所有程序

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

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

它给出了想要的结果

在这种情况下,你总是使用带有分区的排序函数,很可能是行数