简体   繁体   中英

Mysql delete close duplicates of a field

In MySQL, is it possible to delete where a field is equal to another row with a prefix?

For example (psuedo-code):

DELETE from `table` WHERE `field` = CONCAT("duplicate-of-", field)

Assuming I have have these rows, I'd like to delete all of the corresponding duplicates which have that prefix

+ Field                 +
------------------------
| name               |
| duplicate-of-name  |
| name2              |
| duplicate-of-name2 |

You can solve this with the exists operator:

DELETE
FROM   `table` t1
WHERE  EXISTS (SELECT *
               FROM   `table` t2
               WHERE  t2.`field` = CONCAT("duplicate-of-", t1.field))

You can do this with a join :

DELETE tdup
    FROM table tdup JOIN
         table t
         ON tdup.field = CONCAT('duplicate-of-', t.field);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM