A
A
Anton Shcherbakov2015-12-20 13:34:44
PHP
Anton Shcherbakov, 2015-12-20 13:34:44

Search and replace in MySQL table, is there an optimal solution?

There is a translation table with fields:

INSERT INTO `translate` (`translate_id`, `title_ru`, `title_en`, `type`, `cat_id`) 
VALUES
(1,   'абц 0001',   'abc 0001',   1,   10),
(2,   'абц 0001',   'abc 0002',   1,   10),
(3,   'деф 0001',   'def 0001',   2,   20),
(4,   'деф 0002',   'def 0002',   2,   20),
(5,   'деф 0001',   'def 0001',   3,   30),
(6,   'деф 0001',   'def 0002',   3,   30);

Indices:
PRIMARY	translate_id
UNIQUE	title_en, type, cat_id

title_en - always a unique value, but bound to a category (cat_id) and a type (type). Those. if you remove the dependence of these two parameters, duplicates will begin to appear.
Now the problem is on the example of translate_id 3,4,5,6. I do a replacement for the title_en field from 'def 0001' to 'def 0002' and eventually get duplicates and, accordingly, an error.
What is the best way to implement the duplicate merging mechanism? For example, after searching and replacing, get:
INSERT INTO `translate` (`translate_id`, `title_ru`, `title_en`, `type`, `cat_id`) 
VALUES
(1,   'абц 0001',   'abc 0001',   1,   10),
(2,   'абц 0001',   'abc 0002',   1,   10),
(3,   'деф 0001',   'def 0002',   2,   20),
/*(4,   'деф 0002',   'def 0002',   2,   20), дублирующаяся запись была удалена*/
(5,   'деф 0001',   'def 0002',   3,   30);
/*(6,   'деф 0001',   'def 0002',   3,   30); дублирующаяся запись была удалена*/

Thanks in advance for your replies!

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question