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

发布于 2024-11-25 01:05:04 字数 778 浏览 1 评论 0原文

可能的重复:
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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

流心雨 2024-12-02 01:05:04

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

基本上,您无法删除(修改)在 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
                        )

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.

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
                        )
吖咩 2024-12-02 01:05:04

最简单的方法之一是从第一个查询创建一个临时表,然后在第二个语句中删除临时表中不存在的所有课程。

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.

破晓 2024-12-02 01:05:04
  $result=mysql_query("select CourseName from TAB group by CourseName having count(CourseName) < 100");
while($row=mysql_fetch_array($result)){
mysql_query("delete from tab where courses='$row[0]'");
}
  $result=mysql_query("select CourseName from TAB group by CourseName having count(CourseName) < 100");
while($row=mysql_fetch_array($result)){
mysql_query("delete from tab where courses='$row[0]'");
}
若有似无的小暗淡 2024-12-02 01:05:04

也许这可能有效?

DELETE FROM tab AS a 
INNER JOIN (select CourseName from TAB group by CourseName having count(Coursename)>100) as b
ON a.CourseName = b.coursename

Perhaps this may work?

DELETE FROM tab AS a 
INNER JOIN (select CourseName from TAB group by CourseName having count(Coursename)>100) as b
ON a.CourseName = b.coursename
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文