我不是 100% 确定你想在这里输出什么。
但是,假设您想要所有用户及其百分比的列表,以及所有用户及其所有成绩的列表,以及该用户的最后考试日期和最高总分:-
SELECT exam_date.user, English.percentage, Biology.percentage, Maths.percentage, Science.percentage, SocialStudies.percentage, exam_date.ExamDate, total_marks.TotalMarks
FROM (select user, MAX(exam_date) AS ExamDate from exams_db WHERE user = '$userid' GROUP BY user) as exam_date
LEFT OUTER JOIN (select user, percentage from exams_db where subjecttype = 'English' GROUP BY user) as English ON exam_date.user = English.user
LEFT OUTER JOIN (select user, percentage from exams_db where subjecttype = 'Biology' GROUP BY user) as Biology ON exam_date.user = Biology.user
LEFT OUTER JOIN (select user, percentage from exams_db where subjecttype = 'Maths' GROUP BY user) as Maths ON exam_date.user = Maths.user
LEFT OUTER JOIN (select user, percentage from exams_db where subjecttype = 'Science' GROUP BY user) as Science ON exam_date.user = Science.user
LEFT OUTER JOIN (select user, percentage from exams_db where subjecttype = 'SocialStudies' GROUP BY user) as SocialStudies ON exam_date.user = SocialStudies.user
LEFT OUTER JOIN (select user, MAX(total_marks) AS TotalMarks from exams_db WHERE user = '$userid' GROUP BY user) as total_marks ON exam_date.user = total_marks.user
UNION ALL
SELECT exam_date.user, English.percentage, Biology.percentage, Maths.percentage, Science.percentage, SocialStudies.percentage, exam_date.ExamDate, total_marks.TotalMarks
FROM (select user, MAX(exam_date) AS ExamDate from exams_db WHERE user = '$userid' GROUP BY user) as exam_date
LEFT OUTER JOIN (select user, grade from exams_db where subjecttype = 'English' GROUP BY user) as English ON exam_date.user = English.user
LEFT OUTER JOIN (select user, grade from exams_db where subjecttype = 'Biology' GROUP BY user) as Biology ON exam_date.user = Biology.user
LEFT OUTER JOIN (select user, grade from exams_db where subjecttype = 'Maths' GROUP BY user) as Maths ON exam_date.user = Maths.user
LEFT OUTER JOIN (select user, grade from exams_db where subjecttype = 'Science' GROUP BY user) as Science ON exam_date.user = Science.user
LEFT OUTER JOIN (select user, grade from exams_db where subjecttype = 'SocialStudies' GROUP BY user) as SocialStudies ON exam_date.user = SocialStudies.user
LEFT OUTER JOIN (select user, MAX(total_marks) AS TotalMarks from exams_db WHERE user = '$userid' GROUP BY user) as total_marks ON exam_date.user = total_marks.user
如果这不是您想要的,您可以不在代码中解释它。例如,你有学生桌吗?
编辑 - 对于用户列表(已完成考试),我认为以下可能会这样做: -
SELECT exam_date.user,
English.percentage,
Biology.percentage,
Maths.percentage,
Science.percentage,
SocialStudies.percentage,
English.grade,
Biology.grade,
Maths.grade,
Science.grade,
SocialStudies.grade,
exam_date.ExamDate,
total_marks.TotalMarks
FROM (SELECT user, MAX(exam_date) AS ExamDate FROM exams_db GROUP BY user) as exam_date
LEFT OUTER JOIN (SELECT user, percentage, grade FROM exams_db where subjecttype = 'English' GROUP BY user) as English ON exam_date.user = English.user
LEFT OUTER JOIN (SELECT user, percentage, grade FROM exams_db where subjecttype = 'Biology' GROUP BY user) as Biology ON exam_date.user = Biology.user
LEFT OUTER JOIN (SELECT user, percentage, grade FROM exams_db where subjecttype = 'Maths' GROUP BY user) as Maths ON exam_date.user = Maths.user
LEFT OUTER JOIN (SELECT user, percentage, grade FROM exams_db where subjecttype = 'Science' GROUP BY user) as Science ON exam_date.user = Science.user
LEFT OUTER JOIN (SELECT user, percentage, grade FROM exams_db where subjecttype = 'SocialStudies' GROUP BY user) as SocialStudies ON exam_date.user = SocialStudies.user
LEFT OUTER JOIN (SELECT user, MAX(total_marks) AS TotalMarks FROM exams_db GROUP BY user) as total_marks ON exam_date.user = total_marks.user
请注意,我对 total_marks 字段有点怀疑(您的原始查询刚刚获得最高总分)。
可能更有效(但完全未经测试)将是: -
SELECT users.user,
exam_dates.ExamDate,
total_marks.TotalMarks,
MAX(IF(Sub1.subjecttype='English', exams_db.percentage, 0)) AS EnglishPercentage,
MAX(IF(Sub1.subjecttype='English', exams_db.grade, 0)) AS EnglishGrade,
MAX(IF(Sub1.subjecttype='Biology', exams_db.percentage, 0)) AS BiologyPercentage,
MAX(IF(Sub1.subjecttype='Biology', exams_db.grade, 0)) AS BiologyGrade,
MAX(IF(Sub1.subjecttype='Maths', exams_db.percentage, 0)) AS MathsPercentage,
MAX(IF(Sub1.subjecttype='Maths', exams_db.grade, 0)) AS MathsGrade,
MAX(IF(Sub1.subjecttype='Science', exams_db.percentage, 0)) AS SciencePercentage,
MAX(IF(Sub1.subjecttype='Science', exams_db.grade, 0)) AS ScienceGrade,
MAX(IF(Sub1.subjecttype='SocialStudies', exams_db.percentage, 0)) AS SocialStudiesPercentage,
MAX(IF(Sub1.subjecttype='SocialStudies', exams_db.grade, 0)) AS SocialStudiesGrade
FROM users
LEFT OUTER JOIN (
SELECT user, subjecttype, MAX(exam_date) AS LatestSubjectExam
FROM exams_db
WHERE subjecttype IN ('English', 'Biology', 'Maths', 'Science', 'SocialStudies')
GROUP BY user, subjecttype
) Sub1 ON users.user = Sub1.user
LEFT OUTER JOIN exams_db ON Sub1.user = exams_db.user AND Sub1.subjecttype = exams_db.subjecttype AND Sub1.LatestSubjectExam = exams_db.exam_date
LEFT OUTER JOIN (SELECT user, MAX(total_marks) AS TotalMarks FROM exams_db GROUP BY user) total_marks ON users.user = total_marks.user
LEFT OUTER JOIN (SELECT user, MAX(exam_date) AS ExamDate FROM exams_db GROUP BY user) exam_dates ON users.user = exam_dates.user
GROUP BY users.user, exam_dates.ExamDate, total_marks.TotalMarks
再次编辑
尝试和应对第二式查询中的重复用户/考试
SELECT exam_date.user,
English.percentage,
Biology.percentage,
Maths.percentage,
Science.percentage,
SocialStudies.percentage,
English.grade,
Biology.grade,
Maths.grade,
Science.grade,
SocialStudies.grade,
exam_date.ExamDate,
total_marks.TotalMarks
FROM (SELECT user, MAX(exam_date) AS ExamDate FROM exams_db GROUP BY user) as exam_date
LEFT OUTER JOIN (
SELECT user, subjecttype, MAX(exam_date) AS LatestSubjectExam
FROM exams_db
WHERE subjecttype IN ('English', 'Biology', 'Maths', 'Science', 'SocialStudies')
GROUP BY user, subjecttype
) Sub1 ON exam_date.user = Sub1.user
LEFT OUTER JOIN (SELECT user, exam_date, percentage, grade FROM exams_db where subjecttype = 'English' GROUP BY user, exam_date) as English ON exam_date.user = English.user AND Sub1.subjecttype = 'English' AND Sub1.LatestSubjectExam = English.exam_date
LEFT OUTER JOIN (SELECT user, exam_date, percentage, grade FROM exams_db where subjecttype = 'Biology' GROUP BY user, exam_date) as Biology ON exam_date.user = Biology.user AND Sub1.subjecttype = 'Biology' AND Sub1.LatestSubjectExam = Biology.exam_date
LEFT OUTER JOIN (SELECT user, exam_date, percentage, grade FROM exams_db where subjecttype = 'Maths' GROUP BY user, exam_date) as Maths ON exam_date.user = Maths.user AND Sub1.subjecttype = 'Maths' AND Sub1.LatestSubjectExam = Maths.exam_date
LEFT OUTER JOIN (SELECT user, exam_date, percentage, grade FROM exams_db where subjecttype = 'Science' GROUP BY user, exam_date) as Science ON exam_date.user = Science.user AND Sub1.subjecttype = 'Science' AND Sub1.LatestSubjectExam = Science.exam_date
LEFT OUTER JOIN (SELECT user, exam_date, percentage, grade FROM exams_db where subjecttype = 'SocialStudies' GROUP BY user, exam_date) as SocialStudies ON exam_date.user = SocialStudies.user AND Sub1.subjecttype = 'SocialStudies' AND Sub1.LatestSubjectExam = SocialStudies.exam_date
LEFT OUTER JOIN (SELECT user, MAX(total_marks) AS TotalMarks FROM exams_db GROUP BY user) as total_marks ON exam_date.user = total_marks.user