简体   繁体   English

结果太多-SQL连接问题

[英]Too many results - SQL join issues

I've never posted here, so hoping this goes well :). 我从来没有在这里发布过,所以希望一切顺利:)。 I am using SQL Server 2008 R2 and I am trying to extract Student/Course information from a database. 我正在使用SQL Server 2008 R2,并且试图从数据库中提取学生/课程信息。 Here is what I am dealing with: 这是我正在处理的:

Table dbo.StudentsSchedule 表dbo.StudentsSchedule

StudentsSchedule table contains the following fields: CRS_CODE CRS_SECT ID_NUMBER StudentsSchedule表包含以下字段:CRS_CODE CRS_SECT ID_NUMBER

Table dbo.StaffSchedule: 表dbo.StaffSchedule:

COURSE SECTION Teacher_ID 课程部分Teacher_ID

Here is what I am trying to accomplish: 这是我要完成的工作:

I would like to combine the COURSE+SECTION CODE+Teacher_ID to use that as a Unique "New Course ID" and then attach a Student to it. 我想将COURSE + SECTION CODE + Teacher_ID组合起来用作唯一的“ New Course ID”,然后将一个Student附加到它上。

I've attempted this but I am getting way too many results. 我已经尝试过了,但是结果太多了。 I am expecting around 300 but receiving over 10K+ 我预计会达到300,但会收到1万+

 SELECT dbo.StaffSchedule.COURSE+'-'+ dbo.StaffSchedule.SECTION+'-'+dbo.StaffSchedule.Teacher_ID) as [NewCourseID],
          dbo.StudentSchedule.ID_NUMBER AS [StudentID],
          from dbo.StaffSchedule INNER JOIN dbo.StudentSchedule ON
   dbo.StaffSchedule.COURSE=dbo.StudentSchedule.CRS_CODE and
   dbo.StaffSchedule.SECTION=dbo.StudentSchedule.CRS_SECT

Note that Teacher_ID can only exist once per COURSE+SECTION. 请注意,每个COURSE + SECTION只能存在一次Teacher_ID。

Any ideas? 有任何想法吗? Am I doing this wrong? 我做错了吗?

Lets try to see it 让我们尝试看看

Table dbo.StaffSchedule: 表dbo.StaffSchedule:

COURSE SECTION Teacher_ID

  1       1      23
  1       1      24
  1       3      55
  1       3      24

Table dbo.StudentsSchedule: 表dbo.StudentsSchedule:

CRS_CODE CRS_SECT ID_NUMBER

   1        1       44
   1        1       45
   1        3       89
   1        3       44

This code: 这段代码:

   SELECT dbo.StaffSchedule.COURSE+'-'+ dbo.StaffSchedule.SECTION+'-'+dbo.StaffSchedule.Teacher_ID) as [NewCourseID], dbo.StudentSchedule.ID_NUMBER AS [StudentID]
   from dbo.StaffSchedule INNER JOIN dbo.StudentSchedule ON
   dbo.StaffSchedule.COURSE=dbo.StudentSchedule.CRS_CODE and
   dbo.StaffSchedule.SECTION=dbo.StudentSchedule.CRS_SECT

will return: 将返回:

NewCourseID StudentID

   1-1-23        44
   1-1-23        45
   1-1-24        44
   1-1-24        45
   1-3-55        89
   1-3-24        89
   1-3-55        44
   1-3-24        44

May be the only problem I see is that for the same StudentID you have more than one value. 我看到的唯一问题可能是,对于同一个StudentID,您拥有多个值。 I don't know what are you specting, so this is hoy much I can help you. 我不知道您在说什么,所以我可以为您提供很多帮助。

Sorry for my bad english! 对不起,我的英语不好! I hope this can help! 希望对您有所帮助!

EDITE FOR NEW COMMENT 编辑以发表新评论

Is really simple to not use de Section in the NewCourseId 在NewCourseId中不使用de Section真的很简单

   SELECT dbo.StaffSchedule.COURSE+'-'+dbo.StaffSchedule.Teacher_ID) as [NewCourseID],   dbo.StudentSchedule.ID_NUMBER AS [StudentID]
   from dbo.StaffSchedule INNER JOIN dbo.StudentSchedule ON
   dbo.StaffSchedule.COURSE=dbo.StudentSchedule.CRS_CODE and
   dbo.StaffSchedule.SECTION=dbo.StudentSchedule.CRS_SECT

But... what will happen? 但是...会发生什么? The result will be: 结果将是:

NewCourseID StudentID

   1-23        44
   1-23        45
   1-24        44
   1-24        45
   1-55        89
   1-24        89
   1-55        44
   1-24        44

Look what happened whit the ID 1-24, it's look to has duplicated values, do you understand why? 看看ID 1-24发生了什么,看起来值重复,您知道为什么吗? Look at the original tables again. 再次查看原始表。

Sorry for my bad english! 对不起,我的英语不好!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM