mysql嵌套查询获取语法错误

时间:2014-06-06 07:01:27

标签: mysql

我的问题是:

  1. 学生成绩最高的学生。
  2. 学生的注册人 主题恶习最高。
  3. 存在一个带有属性的表“学生”(reg_id,subject,marks)。

    共有8个科目,每个学生全部攻读8个科目。因此,该表包含每个学生8个记录。学生的数量取决于。例如:10名学生,总数为no.of.records = 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
    

    第二个,我得到答案,但没有正确的价值。

2 个答案:

答案 0 :(得分:0)

你改变如下:

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);

答案 1 :(得分:0)

你可以试试这个。

      Select reg_id, max (total) as maxtotal from (
        select reg_id, sum(marks) as total from students S    group by reg_id ) t