How do i make sql syntax on mysql, when i want to update parent_id based on id on the other record on the same table with the same value on specific column field, example field Code
i tried to make the following
update product_class t1
set t1.parent_id = t2.id
WHERE t1.family_code <>'' and t1.class_code = ''
join product_class t2
on
(t1.segment_code = t2.segment_code)
but gives me error
Here is the table structure:
Here is the correct syntax:
update product_class t1 join
product_class t2
on t1.segment_code = t2.segment_code
set t1.parent_id = t2.id
where t1.family_code <> '' and t1.class_code = '';
The join
is part of the update
clause in MySQL.
NOTE: the query doesn't look like it would do the right thing. You are doing a self-join on what looks like a non-unique column, which will generate lots of matches. An arbitrary matching row would then be used for the update
.
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.