查询输出的行数多于应该执行的行数,为什么会这样?

时间:2011-10-25 22:17:27

标签: php mysql database

我有一个表单,在提交时,它会输出输入到表单中的数据并将其输出到查询结果中。如果文本框为空,则搜索该特定字段的所有数据。目前我的查询正在输出:

SessionId     ModuleId     CourseId   ClassId    StudentUsername  TeacherUsername   Grade
ABB           CHT2520      INFO101    104        u0867587         m.prigmore        A
ABB           CHI2550      INFO101    101        u0867587         j.lu              A
ABB           CHI2550      INFO101    104        u1231231         j.lu              F
ABB           CHT2520      INFO101    101        u1231231         m.prigmore        F
NVK           CHI2550      INFO101    101        u0867587         j.lu              F
RMI           CHI2565      INFO101    102        u0660663         j.forden          F

它输出的行数应该更多。一个学生属于一个班级的一个班级。问题是它显示一个学生属于两个类,一个模块不正确。这样做的原因是因为它根据学生正在学习的模块来识别学生所在的课程。

因此,如果模块“CHI2550”有两个类,那么任何学习“CHI2550”模块的学生都应分配给两个班级,因为应该将一个学生分配给一个班级用于一个班级,这应该由''确定StudentClass'表。问题是,如果我在其中一个JOINS中包含'StudentClass',那么查询将输出9行。

查询应输出:

  SessionId     ModuleId     CourseId   ClassId    StudentUsername  TeacherUsername   Grade
    ABB           CHI2550      INFO101    101        u0867587         j.lu              A
    ABB           CHI2550      INFO101    104        u1231231         j.lu              F
    NVK           CHT2520      INFO101    103        u0867587         m.prigmore        F
    RMI           CHI2565      INFO101    102        u0660663         j.forden          F

如何让查询输出上面的结果?以下是查询:

 SELECT * FROM Course c
            INNER JOIN CourseModule cm ON c.CourseId = cm.CourseId
            JOIN Module m ON cm.ModuleId = m.ModuleId
            JOIN Class cl ON m.ModuleId = cl.ModuleId 
            JOIN Teacher t ON cl.TeacherId = t.TeacherId 
            JOIN Session s ON m.ModuleId = s.ModuleId
            JOIN Grade_Report gr ON s.SessionId = gr.SessionId
            JOIN Student st ON gr.StudentId = st.StudentId
          WHERE
            ('".mysql_real_escape_string($sessionid)."' = '' OR gr.SessionId = '".mysql_real_escape_string($sessionid)."')
          AND
            ('".mysql_real_escape_string($moduleid)."' = '' OR s.ModuleId = '".mysql_real_escape_string($moduleid)."')
          AND
            ('".mysql_real_escape_string($courseid)."' = '' OR c.CourseId = '".mysql_real_escape_string($courseid)."')
          AND
            ('".mysql_real_escape_string($classid)."' = '' OR cl.ClassId = '".mysql_real_escape_string($classid)."')
          AND
            ('".mysql_real_escape_string($teacherid)."' = '' OR t.TeacherUsername = '".mysql_real_escape_string($teacherid)."')
          AND
            ('".mysql_real_escape_string($studentid)."' = '' OR st.StudentUsername = '".mysql_real_escape_string($studentid)."')
          AND
            ('".mysql_real_escape_string($year)."' = '' OR st.Year = '".mysql_real_escape_string($year)."')
          AND
            ('".mysql_real_escape_string($grade)."' = '' OR gr.Grade = '".mysql_real_escape_string($grade)."')

下面是每个表及其数据(我面临的问题的最重要的表是StudentClass,Class,Student,Module):

Course Table:

CourseId            CourseName                                          Duration 
INFO101         Bsc Information Communication Technology (ICT)          3/4

CourseModule Table:

CourseId      ModuleId
INFO101       CHI2550
INFO101       CHI2565
INFO101       CHT2520

Module Table:

ModuleId            ModuleName                                 Credits
CHT2520          Advanced Web Programming                       20
CHI2565          E-Commerce Business and Technology             20
CHI2550          Modern Database Applications                   20

Session Table:

SessionId   ModuleId   Semester   SessionDate   SessionTime   TeacherId  Room     Building

NVQ        CHT2520      Spring    2011-03-21    13:00:00      T2       CW2/04   Canalside West
NVK        CHT2520      Fall      2011-11-10    10:00:00      T2       CW2/04   Canalside West
RMI        CHI2565      Fall      2011-09-13    12:00:00      T5       CW5/01   Canalside West
RMT        CHI2565      Spring    2011-03-29    14:00:00      T2       CW2/04   Canalside West
ABB        CHI2550      Spring    2011-03-15    12:00:00      T1       CW4/10   Canalside West
ABH        CHI2550      Summer    2011-05-03    15:00:00      T4       CW4/10   Canalside West

Grade_Report Table:

StudentId  SessionId  Grade  Mark 
S1          NVK         F      5
S2          ABB         A      80
S1          ABB         A      80
S3          RMI         F      0

 Student Table:

StudentId  StudentForename     StudentSurname    Year   StudentUsername    CourseId 
S1         Mayur                 Patel            3       u0867587          INFO101
S2         Jim                   Carlton          3       u1231231          INFO101
S3         Ahmed                 Seedat           3       u0660663          INFO101
S4         Amar                  Barot            3       u0954857          INFO101
S5         Richard               Davies           3       u0877223          INFO101

StudentClass Table:

ClassId  StudentId
101        S1
102        S3
103        S1
104        S2


    Class Table:

ClassId    Room      ClassDay      ClassTime      ModuleId      CourseId      TeacherId 
101        CW4/10    Thursday       10:00:00      CHI2550       INFO101         T1
102        CW5/01    Wednesday      12:00:00      CHI2565       INFO101         T5
103        CW2/04    Monday         15:00:00      CHT2520       INFO101         T2
104        CW4/10    Thursday       11:00:00      CHI2550       INFO101         T1


Teacher Table:

TeacherId  TeacherForename  TeacherSurname  TeacherUsername  TeacherPassword 
T1          Joan            Lu              j.lu             scomp21
T2          Martyn          Prigmore        m.prigmore       prigmore36
T3          Arshard         Ali             a.ali            aliict
T4          Paul            Judge           p.judge          data01
T5          John            Forden          j.forden         hudds_10

1 个答案:

答案 0 :(得分:0)

我首先将查询更改为单个表查询,并确保从中获得所需内容,然后一次添加一个表,直到重复行开始出现。

我最好的猜测是你有一对表需要连接两个字段而不是一个字段,并且单字段连接匹配连接表中的多个行,因此产生额外的行你的输出。

另一种可能性是,您的某个表中包含重复的行信息,导致重复的结果。

您的CourseModule表可能是两种情况下的问题......

很难从你的问题中看到确切的解决方案,因为有很多表格和很多连接,所以很好地问了它...