我的问题是:
- 总分最高的学生的 reg_id。
- 学生的 reg_ids who 科目恶习得分最高。
存在一个带有属性 (reg_id,subject,marks) 的表“students”。
有8个科目,每个学生选修全部8个科目。因此该表包含每个学生 8 条记录。取决于学生的数量。例如:10 名学生,因此总记录数 = 8*10 =80。
这是我的疑问。请有人纠正其中的错误。谢谢。
1)"select A.reg_id from (select S.reg_id, sum(S.marks) as total from students S group by S.reg_id) as A where A.total=(select max(C.total) from (select S1.reg_id,sum(S1.marks) as total from students S1 group by S1.reg_id) as C);"
2)"select A.reg_id from (select S.reg_id, sum(S.marks) as total from students S group by S.reg_id) as A where A.total=(select max(A.total) from A);"`
错误消息:
ERROR 1064 (HY000) at line 1: You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'select S.reg_id, sum(S.marks) as total from
students S group by
Student's reg_id who took highest total marks :
Students' reg_ids who took highest for subject vice : reg_id subject max(s.marks) id1 Buddhist 99 id1 computer 81 id1 English 95 id5 History 82 id1 Literature 95 id1 Maths 97 id1 Sinhala 85 id9 Social 90
第二个,我得到了答案,但没有正确的值。
最佳答案
你改变如下:
select A.reg_id from (select S.reg_id, sum(S.marks) as total from students S group by S.reg_id) as A where A.total IN (select max(C.total) from (select S1.reg_id,sum(S1.marks) as total from students S1 group by S1.reg_id) as C);
select A.reg_id from (select S.reg_id, sum(S.marks) as total from students S group by S.reg_id) as A where A.total IN (select max(A.total) from A);
关于mysql 嵌套查询出现语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24076061/