MySQL嵌套SELECT问题

时间:2018-12-04 18:31:32

标签: mysql

我对嵌套SELECT有问题 我收到以下错误消息:“选择对于此服务器版本在此位置无效,期望使用'('与”

SELECT group_id, count (student_id) 
FROM
(
SELECT student.group_id, student.student_id, AVG(mark)
    FROM
    student, group, result
    WHERE
        student.student_id = result.student_id 
        and
        student.group_id = group.group_id
        GROUP BY student.group_id, student.student_id
) AS ”p”
p.AVG > 4
GROUP BY group_id
ORDER BY count (student_id) DESC
LIMIT 1;

,我不知道如何解决。请帮帮我!

预先感谢您的回答

1 个答案:

答案 0 :(得分:0)

我将从修复两件事开始:

  • group是保留字。如果将它用作表名(尽量不要使用),则需要将其引用。
  • 将表达式AVG(mark)命名为avg_mark
  • 添加WHERE子句。

尝试以下查询:

SELECT group_id, count (student_id) 
FROM
(
SELECT student.group_id, student.student_id, AVG(mark) as avg_mark
    FROM
    student, `group`, result
    WHERE
        student.student_id = result.student_id 
        and
        student.group_id = `group`.group_id
        GROUP BY student.group_id, student.student_id
) p
WHERE avg_mark > 4
GROUP BY group_id
ORDER BY count (student_id) DESC
LIMIT 1;