我正在运行以下查询:
select students.name, students.rollno, students.category as cat, `t1`.`studentid`
AS `studentid`,
min(`t1`.`paperid`) AS `paper1`,
min(`t2`.`paperid`) AS `paper2`,
min(`t3`.`paperid`) AS `paper3`,
min(`t4`.`paperid`) AS `paper4` from students,
((((`sps` `t1` left join `sps` `t2` on(((`t1`.`studentid` = `t2`.`studentid`) and
(`t2`.`paperid` > `t1`.`paperid`)))) left join `sps` `t3` on(((`t2`.`studentid` =
`t3`.`studentid`) and (`t3`.`paperid` > `t2`.`paperid`)))) left join `sps` `t4`
on(((`t3`.`studentid` = `t4`.`studentid`) and (`t4`.`paperid` > `t3`.`paperid`))))
left join sgm on sgm.studentid=t1.studentid
left join str on str.studentid=t1.studentid
left join papers on t1.paperid=papers.p_id and papers.type='optional'
left join `students` `stu` on((`stu`.`studentid` = `t1`.`studentid`)))
where ((`stu`.`course` = '1')
and (`stu`.`status` = 'regular')
and students.studentid=t1.studentid)
and papers.type='optional'
and sgm.groupid='1'
and str.semesterid='1'
and str.sessionid='13'
group by `t1`.`studentid`
order by name
我在这里要做的是,我从表中获取结果:
Students Table
| studentid | name | rollno | category | course |
1 ABC 1 new 1
2 BCD 2 old 1
3 CDE 3 new 1
Papers Table
| p_id | p_name | type |
p1 a main
p2 b optional
p3 c main
p4 d optional
p5 e optional
SGM
| id | studentid | group |
1 1 a
2 2 a
3 3 b
SPS
| id | studentid | paper |
1 1 p1
2 1 p3
3 1 p4
4 1 p5
5 2 p3
6 2 p4
7 2 p5
8 3 p1
这些是学生,SPS,SGM,纸张表。 在查询中我想要找出的是每个学生应该有一个结果应该有信息,名称,rollno,cat,studentid,paper1,paper2,paper3和paper4。我能够做到这一点,但我只能获得那些类型是可选的纸张。我只需要选择可选纸张。类型是paperid,分配给每个学生的论文是sps。我怎么能这样做?
如果没有分配论文,则应该存在空值。