PHP:显示所有学生标记与主题列

时间:2015-08-20 05:25:43

标签: php mysql

我有3张桌子student_register, subject, marks必须显示所有带有主题栏的学生标记 使用student_register_id标记表,subject_id 我想以表格格式显示输出告诉我mysql查询。

学生姓名Sub1 Sub2 Sub3 Sub4 Sub5 Sub6 Outof

1        Akash       75    80   82   86    70    71    100

2        Amit        92     87  89   93    65     82   100

我的查询是

SELECT DISTINCT student_register.student_register_id,student_register.student_first_name, student_register.stud_l_name, subject.subject_name, subject.subject_id, marks.marks_obtain,marks.outof, marks.status 
FROM student_register 
INNER JOIN marks ON  marks.student_register_id=student_register.student_register_id

INNER JOIN subject ON marks.subject_id=subject.subject_id 
INNER JOIN class_teacher ON class_teacher.division_id = student_register.division_id AND class_teacher.class_teacher_id = '".$_SESSION['class_teacher_id']."'";

它给我输出

学生姓名Sub1 Sub2 Sub3 Sub4 Sub5 Sub6 Outof

1        Akash           71                            100

2        Akash      92                                 100

3        Akash                       93                100

4        Amit                    98                    100

5        Amit       89                                 100

2 个答案:

答案 0 :(得分:0)

假设您的表结构如下:

student {Studentid, StudentName, ...}
subject {Subjectid, SubjectName, ...}
marks {Studentid, Subjectid, mark , ...}

select Studentid, StudentName, a.mark Sub1, b.mark Sub2, c.mark Sub3, d.mark Sub4, e.mark Sub5, f.mark Sub6, 100 Outof
from student, marks a, marks b, marks c, marks d, marks e, marks f
where student.Studentid = a.Studentid and a.Subjectid = 901
and student.Studentid = b.Studentid and b.Subjectid = 902
and student.Studentid = c.Studentid and c.Subjectid = 903
and student.Studentid = d.Studentid and d.Subjectid = 904
and student.Studentid = e.Studentid and e.Subjectid = 905
and student.Studentid = f.Studentid and f.Subjectid = 906

然而,最好使用php来产生结果。当有空数据时,要避免左连接

答案 1 :(得分:0)

"SELECT s.student_register_id,
       s.student_first_name,
       a.mark Sub1,
       b.mark Sub2,    
       c.mark Sub3,
       d.mark Sub4,
       e.mark Sub5,
       f.mark Sub6,
       100 Outof
FROM student_register s, marks a, marks b, marks c, marks d, marks e, marks f
INNER JOIN class_teacher k ON k.division_id=s.division_id
WHERE (s.student_register_id = a.student_register_id AND a.Subjectid = 100)
AND   (s.student_register_id = b.student_register_id AND b.Subjectid = 102)
AND   (s.student_register_id = c.student_register_id AND c.Subjectid = 103)
AND   (s.student_register_id = d.student_register_id AND d.Subjectid = 104)
AND   (s.student_register_id = e.student_register_id AND e.Subjectid = 105)
AND   (s.student_register_id = f.student_register_id AND f.Subjectid = 106)
AND   k.class_teacher_id='".$_SESSION['class_teacher_id']."'";

你可以试试这个