如何使用mySQL使用group by删除数据库中的记录
可能的重复:
SQL 删除:无法指定目标表更新 FROM 子句
我只有一张表(将此表称为 TAB),代表大学考试。我有以下属性:课程名称、课程代码和年份。我想删除基数小于 100 的所有课程。如果我输入,
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);
,但系统会返回错误:
错误代码:1093 您无法在 FROM 子句中指定要更新的目标表“TAB”
我必须如何删除这些记录?
Possible Duplicate:
SQL Delete: can't specify target table for update in FROM clause
I have one table only (call this table TAB), representing University exams. I have the following attributes: CourseName, CourseCode and year. I want to delete all courses that have a cardinality less than 100. If I type
select CourseName from TAB group by CourseName having count(CourseName) < 100;
I have an exact result. But if I want to delete this entries I try with
delete from TAB where CourseName not in (select CourseName from TAB group by CourseName having count(CourseName) > 100);
but the system returns an error:
Error Code: 1093 You can't specify target table 'TAB' for update in FROM clause
How I have to delete these records?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
请参阅以下链接中的答案。它将解决您的问题:
基本上,您无法删除(修改)在 SELECT 中使用的同一个表。该页面记录了一些解决方法。
下面的代码将通过使嵌套的
select
成为临时表来工作。Please see the answer at the following link. It will solve your issue:
Basically, you can't delete from (modify) the same table you use in the SELECT. There are ways around it documented at that page.
The following will work by making your nested
select
a temp table.最简单的方法之一是从第一个查询创建一个临时表,然后在第二个语句中删除临时表中不存在的所有课程。
One of the easiest ways is to create a temporary table from your first query, and then in a second statement delete all the courses not present in the temporary table.
也许这可能有效?
Perhaps this may work?