mysql 嵌套查询出现语法错误

标签 mysql

我的问题是:

  1. 总分最高的学生的 reg_id。
  2. 学生的 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/

相关文章:

php - php/mysql 中 "bring up my post"的最佳方法?

php - 如何搭建一个带有倒计时功能的mysql排队系统?

php - 用ajax覆盖同一行

MYSQL:您能否提取匹配 4 个表达式中的 3 个的结果?

mysql - NULL 值的空间?

mysql - 如果一条数据记录不存在,则将其存储到另一个表中

mysql - 如何在mySQL中并排显示项目、组成员和他们的老板

php - 当我使用 PHP MySQL 查询时,DISTINCT 关键字不起作用

php - 在另一个 php 文件中选择同一个表

mysql - 推荐引擎数据库的设计?