简体   繁体   中英

Mysql query, populate a value based on other

meta_id | post_id  |         meta_key   |      meta_value
----------------------------------------------------------
19368   |    5716  | _wpbdp[fields][8]  |
19369   |    5716  | _wpbdp[fields][9]  |    124_ico.jpg    
19370   |    5716  | _wpbdp[fields][10] |

This is my mysql table. I want that if for a postid the metakey _wpbdp[fields][9] has some value assigned(124_ico.jpg) in this case, then _wpbdp[fields][10] metavalue is updated to 124_img.jpg

Any help with the query would be great.

If you want the '_wpbdp[fields][10]' rows of a given post_id to have the same meta_value as a row for the same post_id where the meta_value is filled in then you can use the following:

update tbl t join (select post_id, meta_value
                     from tbl
                    where meta_value is not null) x on t.post_id = x.post_id
   set t.meta_value = replace(x.meta_value, '_ico', '_img')
 where t.meta_key = '_wpbdp[fields][10]'

See fiddle at: http://sqlfiddle.com/#!2/6656ae/1/0

SELECT @var:= meta_value FROM t WHERE meta_value IS NOT NULL;
SELECT @no:= REPLACE(REPLACE(SUBSTRING_INDEX(meta_key,']',-2),'[',''),']','')+1 FROM t
WHERE meta_value IS NOT NULL;
UPDATE t 
SET meta_value =  @var 
WHERE REPLACE(REPLACE(SUBSTRING_INDEX(meta_key,']',-2),'[',''),']','')=
@no
ORDER BY meta_id

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