Answer the question
In order to leave comments, you need to log in
MySql How to do search vs replace?
Kind
There is a task on processing of several thousand records.
Essence: It is necessary to replace the value of the name in the parent field with the ID
, insert the ID instead of the name in the parent field, if the name in the parent field matches the ID Name
Table example:
(replace Vasya with 1, replace Vassiliy with 11)
ID | Name | Parent
(1, Vasya, Petya),
(2, Misha, Vasya),
(3, Grisha, Vasya),
(4, Pasha, Vasya),
(5, Masha, Vasya),
(6, Serj, Vassiliy),
(7, Ivan1, Vassiliy),
(8, Ivan2, Vassiliy),
(9, Garik, Vassiliy),
(10, Vassiliy, Vassiliy),
(11, Vassiliy, Petrovich),
Answer the question
In order to leave comments, you need to log in
Get used to it, if you have a one-time task - to carry it out with an ax. Here's the truth. The point is to come up with a beautiful script, if you can take it and sort it out by brute force. And figs that the script will work for 10 minutes (although in fact it will do it in a minute.)
In any case, I advise you to add the parent_id field, and not write numbers to parent and manipulate this field. The solution does not always have to be elegant.
Algorithm 1)
Pull out one ID at a time (if 1 is a permanent list), and execute an update request:
"update users set parent_id=3 where Parent=Grisha "
Algorithm 2)
Add a temporary is_read field, where we set the default to 0
You can temporarily add a read mark, then the request will come out faster
and just sort it out.
Algorithm 3)
If you are already good at mysql queries:
UPDATE users i INNER JOIN
users o
ON i.name = o.parent
SET o.parent_id = i.id
the elementary update + join on the same table
for you request chtol to write? these are the basics of the basics
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question