Database tips: delete duplicate records in MySql

Source: Internet
Author: User

Table Structure:
Create table tab_name
(
Id tinyint,
Name varchar (10 ),
Age tinyint,
Phone varchar (15 ));

Original content: select * from tab_name;
Id name age phone
1 zhangsan 10 12345678910
2 lisi 28 98765432110
3 wangwu 33 45678912312
4 zhaoliu 45 78945612354
2 lisi 28 98765432110
3 wangwu 33 45678912312

Search after deduplication: select * from tab_name group by id, name, age, phone;
Id name age phone
1 zhangsan 10 12345678910
2 lisi 28 98765432110
3 wangwu 33 45678912312
4 zhaoliu 45 78945612354

Create another table to save and delete duplicate content: create table temp select * from tab_name group by id, name, age, phone;

Delete original table content: delete from tab_name;

Correct Import Record: insert into tab_name select * from temp;

If you only want to delete some records, you can use the where clause: select * from tab_name where id = 1 group by id, name, age, phone;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.