查询输出的行数超出应有的行数,这是为什么?
我有一个表单,当提交时,它会输出输入到表单中的数据并将其输出到查询结果中。如果文本框为空,它将搜索该特定字段的所有数据。目前我的查询正在输出以下内容:
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
I have a form which when it is submitted, it will output the data inputted into the form and output it in the query result. If a textbox is empty it searches all the data for that particular field. At the moment my query is outputting this:
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
It is outputting more rows then it should do. One student belongs in one class for one module. The problem is that it is showing that one student belongs in two classes for one module which is incorrect. The reason it is doing this is because it recognises which class a student is in depending on the module the student is taking.
So if there are two class for module 'CHI2550', any student that takes module 'CHI2550' are allocated to both classes when it should be one student is allocated to one class for one module and that should be determined by the 'StudentClass' table. Problem is that if I include 'StudentClass' in one of the JOINS then the query outputs 9 rows.
The query should of outputted this:
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
How can I get the query to output the result above? Below is the query:
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)."')
Below are each table and their data (most important tables for the problem I am facing are 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我首先将查询更改为单个表查询,并确保从中获得所需的内容,然后一次添加一个表,直到开始出现重复的行。
我最好的猜测是,您有一对表,您需要在其中连接两个字段而不是一个字段,并且单个字段连接与连接表中的多个行匹配,因此在输出中产生额外的行。
另一种可能性是您的一个表具有重复的行信息,这导致了重复的结果。
您的
CourseModule
表可能是这两种情况下的问题...很难从您的问题中看到确切的解决方案,这是很好的问题,因为有很多表和许多联接...
I would start by changing the query to a single table query, and ensuring you get what you need from that, then add one table in at a time until the duplicate rows start appearing.
My best guess would be that you have one pair of tables where you need to join on two fields rather than one, and that the single field join is matching more than one row in the joined table, hence producing the extra rows in your output.
The other possibility is that you have one of your tables with duplicate row information which is causing the duplicate results.
Your
CourseModule
table may be the problem in both cases...It's difficult to see the exact resolution from your question, well asked that it is, as there are lots of tables and many joins...