MySQL delete duplicate records only keep one bar

Source: Internet
Author: User
Tags mysql delete

Delete duplicate records in a table, leaving only one entry:

Delete  from where inch (Select* from,selectmaxfromgroup by  having Count > 1  as B);

Instance:
2. When you want to create a unique index for a table, you need to delete duplicate records because there are duplicate records in the table that cannot be made.
Example Table Dept

Id_no            id_name        '              AAA ' 101 '        BBB '        102              ' CCC '        103              ' DDD '              EEE '        '              FFF '        103              ' GGG '

Write a (or a set) SQL statement to delete the duplicate rows, but one row is required in the repeating line,
As in the table above, id_no=100 need to delete 2 rows, keep one row, id_no=101 need to delete one row to keep a row id_no=102 and 103 do not delete

Delete  from where  not inch Select *  from (selectmaxfromgroupby haveCount(id_ NO)>1) b);

But my needs are not the same,


| ID | Createtime | Labid | PubId |
+-–+ ——————— + ——-+ ———————————— –+
| 171 | 2014-05-22 21:49:11 | 1 | d4165953-2245-4b8c-b21d-8c08d519f66e |
| 172 | 2014-05-22 21:49:11 | 2 | d4165953-2245-4b8c-b21d-8c08d519f66e |
| 173 | 2014-05-22 21:49:11 | 6 | d4165953-2245-4b8c-b21d-8c08d519f66e |
| 174 | 2014-05-22 21:49:11 | 5 | d4165953-2245-4b8c-b21d-8c08d519f66e |
| 175 | 2014-05-22 21:49:11 | 2 | d4165953-2245-4b8c-b21d-8c08d519f66e |
| 176 | 2014-05-22 21:49:11 | 6 | d4165953-2245-4b8c-b21d-8c08d519f66e |
| 177 | 2014-05-22 21:49:11 | 5 | d4165953-2245-4b8c-b21d-8c08d519f66e |
| 178 | 2014-07-03 15:20:07 | 2 | D5308254-3b52-4ada-8e10-16b6a764c5e5 |
| 179 | 2014-07-03 15:36:00 | 2 | d4bc80b4-b57b-4049-9ab3-8f45102fae91 |
| 180 | 2014-07-04 21:05:26 | 4 | f8ccee8f-d93c-41cc-8d2f-28ada7ea4b07 |
I need to delete the same duplicate records as the Labid and pubid, as long as labid and pubid have a different one than the duplicate records.

Delete  from where  not inch Select *  from (Selectfromgroup by pubid,labid) b);

MySQL delete duplicate records only keep one bar

Related Article

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.