如何删除MySQL表中的所有重复记录?
要从表中删除重复的记录,我们可以使用DELETE命令。现在让我们创建一个表。
mysql> create table DuplicateDeleteDemo -> ( -> id int, -> name varchar(100) -> );
将记录插入表“DuplicateDeleteDemo”:在这里,我们将“John”添加为重复记录3次。
mysql> insert into DuplicateDeleteDemo values(1,'John'); mysql> insert into DuplicateDeleteDemo values(1,'John'); mysql> insert into DuplicateDeleteDemo values(2,'Johnson'); mysql> insert into DuplicateDeleteDemo values(1,'John');
要显示所有记录,请使用SELECT语句。
mysql> select *from DuplicateDeleteDemo;
以下是具有重复记录的输出。
+------+---------+ | id | name | +------+---------+ | 1 | John | | 1 | John | | 2 | Johnson | | 1 | John | +------+---------+ 4 rows in set (0.00 sec)
在上面的输出中,表中有4条记录,其中3条记录是重复的。
要删除重复的记录,请udeDELETE。
mysql> delete from DuplicateDeleteDemo where id=1;
要检查记录是否已删除,让我们再次显示所有记录。
mysql> select *from DuplicateDeleteDemo;
以下输出显示所有重复记录均已删除。
+------+---------+ | id | name | +------+---------+ | 2 | Johnson | +------+---------+ 1 row in set (0.00 sec)
热门推荐
10 新年门口花束祝福语简短
11 盘锦结婚祝福语大全简短
12 父母生日祝福语 简短独特
13 家庭恩爱祝福语简短英文
14 高考俄语祝福语大全简短
15 虎年祝福语 诗句唯美简短
16 生日婚礼祝福语简短精辟
17 虎年喝酒拜年祝福语简短
18 教师闺蜜祝福语简短