我有一个包含这些字段的表格:
id_post
meta_key
meta_value
查询应该获取“name_value”的值,其中“meta_key”匹配给定的文本,并将其从“meta_value”的值中删除,其中“meta_key”是“sku”。
假设我有这些行:
id_post - meta_key - meta_value
101 - myCustomText - train
101 - sku - cartrain
102 - myCustomText - boat
102 - sku - carboat
然后在运行查询后,结果应为:
id_post - meta_key - meta_value
101 - myCustomText - train
101 - sku - 汽车
102 - myCustomText - boat
102 - sku - 汽车
用语言来说,查询应该这样做:
对于具有相同的“id_post”的每个行“meta_value”的选择值WHERE“meta_key”==“myCustomText”并从“meta_value”中获取其价值WHERE“meta_key”==“sku”
答案 0 :(得分:2)
update demo
set sku = replace(sku, name_variation, '')
或者将表格显示为所需的视图
select id_post,
name_variation,
replace(sku, name_variation, '') sku
from demo
修改更新的问题
使用具有相同表格的联接但在on
子句
update demo a
join demo b on (a.id_post = b.id_post and b.meta_key = 'myCustomText' )
set a.meta_value = replace(a.meta_value, b.meta_value, '')
where a.meta_key = 'sku'
select *,
replace(a.meta_value, b.meta_value, '') sku
from demo a
join demo b on (a.id_post = b.id_post and b.meta_key = 'myCustomText' )
where a.meta_key = 'sku'