How to delete records in DB with mySQL using group by [duplicate]

北城余情 提交于 2019-12-04 17:16:35
Devin Burke

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
                        )

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.

  $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]'");
}

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!