mysql - 如何使用mySQL使用group by删除数据库中的记录

标签 mysql sql syntax sql-delete

Possible Duplicate:
SQL Delete: can't specify target table for update in FROM clause

我只有一张表(将此表称为 TAB),代表大学考试。我有以下属性:类(class)名称、类(class)代码和年份。我想删除基数小于 100 的所有类(class)。如果我输入

select CourseName from TAB group by CourseName having count(CourseName) < 100;

我有一个确切的结果。但如果我想删除这些条目,我会尝试使用

delete from TAB where CourseName not in (select CourseName from TAB group by CourseName having count(CourseName) > 100);

但系统返回错误:

Error Code: 1093 You can't specify target table 'TAB' for update in FROM clause

我该如何删除这些记录?

最佳答案

请参阅以下链接的答案。它将解决您的问题:

基本上,您不能删除(修改)在 SELECT 中使用的同一个表。该页面记录了解决该问题的方法。

通过将嵌套的 select 设为临时表,可以实现以下操作。

delete from TAB
where CourseName not in (select temp.CourseName
                         from (select t.CourseName
                               from TAB t
                               group by t.CourseName
                               having count(t.CourseName) > 100
                              ) as temp
                        )

关于mysql - 如何使用mySQL使用group by删除数据库中的记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6764252/

相关文章:

php - 数据未在 php 中使用 $_SESSION 变量插入

php - MySQL 和 PHP 提高性能

mysql - 在数据库表中为 "log of changes"创建表的最佳做法是什么?

c# - EF : How to execute a SQL-query with multiple joins?

c# - VB.net 中的 C# 'internal' 是什么?

c - 将数组传递给函数的正确语法

Java表单搜索方法不起作用-(无法从数据库成员表中细化Member_ID)

php - 使用数据库生成的列表时传递 PHP 变量而不被看到

sql - 如何在 T-SQL 中分配多个内联变量?

mysql - 内连接mysql连接3个表