mysql 去除重复记录的方法

来源:互联网 发布:淘宝美工各种尺寸 编辑:程序博客网 时间:2024/06/06 00:22
18  1   小明20  2   小雷21  1   小明22  1   小明23  2   小雷24  2   小雷

数据中有以上数据,现在需要去除重复的记录,一开始以为可以直接用这段sql:

DELETE  from stu s WHERE s.id in (SELECT max(id) id from stu GROUP BY sid)

===>结果报错:

[Err] 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE s.id in (SELECT max(id) id from stu GROUP BY sid)' at line 1

查资料发现:
数据库现在不支持对一个表进行select等子操作后,然后对该表做delete或者updata这类的操作。
整理思路后写出这样的sql,发现可行,就此记录如下:

DELETE from stu where id not in ( SELECT * from ((SELECT max(id) from stu GROUP BY sname )as a ))

执行后数据库中数据如下:

22  1   小明24  2   小雷
0 0
原创粉丝点击