【问题标题】:Delete from table using subquery from another table使用来自另一个表的子查询从表中删除
【发布时间】:2021-01-18 06:39:22
【问题描述】:

Tables 我需要从students 表中删除所有在exam_results 表中得到低于平均正确答案百分比的学生。必须使用子查询来完成。

到目前为止我已经尝试过:

DELETE FROM students
WHERE id IN (SELECT student_id FROM exam_results WHERE percentage < AVG(percentage));

返回:ERROR 1111: Invalid use of group function

为什么这是对组功能的无效使用? 我错过了什么或有什么问题?

非常感谢任何帮助!谢谢!

【问题讨论】:

    标签: sql subquery sql-delete


    【解决方案1】:

    你需要在sub-query找到学生,如下:

    DELETE FROM students
     WHERE id IN 
       (SELECT student_id 
          FROM exam_results 
         WHERE percentage < (select AVG(percentage) from exam_results);
    

    您也可以使用analytical function,但这取决于您的 MySql 版本。所以请标记您正在使用的数据库及其版本。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-03-14
      • 1970-01-01
      • 1970-01-01
      • 2016-04-07
      • 2021-06-19
      • 2020-08-08
      • 1970-01-01
      相关资源
      最近更新 更多