【问题标题】:Using SQL query to update meta_key='_price' with value in meta_key='_regular_price'使用 SQL 查询使用 meta_key='_regular_price' 中的值更新 meta_key='_price'
【发布时间】:2019-06-07 14:45:24
【问题描述】:

在我想用 _regular_price 的值更新 _price 的每个产品的数据库中,由于 _price 的值小于 _regular_price 的值,我尝试使用此查询但没有成功。

UPDATE wp_g9he6fnfw8_postmeta 
SET META_KEY._regular_price = META_KEY._price

我也尝试过像这样更新值但没有用

UPDATE wp_g9he6fnfw8_postmeta 
SET _regular_price = _price

我哪里出错了。如何将 _regular_price 的值复制到 _price。

【问题讨论】:

  • 第一个查询试图更新一个未在查询的更新表部分中引用的表(meta_key),如果 wp_g9he6fnfw8_postmeta 中存在 _regular_price 和 _price,则第二个查询看起来应该有效。您能否详细说明无效的含义 - 您是否收到语法错误或查询是否按预期运行?
  • 我收到未知列错误,如何定位 _price ?
  • _price 存在于哪个表中?
  • 这是截图中的表结构(prnt.sc/m6j906),表的名称是 wp_g9he6fnfw8_postmeta 列的名称是 Meta_Key 并且在 Meta_key 列行中我需要更新 _price 值与值_regular_price

标签: mysql sql wordpress woocommerce


【解决方案1】:

例如,您可能需要自加入

drop table if exists t;

create table t( postid int, meta_key varchar(20), meta_value int);

insert into t values
(1,'_regular_price',10),
(1,'_price',20);

update t t1 join t t2 on t2.postid = t1.postid and t2.meta_key = '_price'
set t1.meta_value = t2.meta_value
where t1.meta_key = '_regular_price';

select * from t;

+--------+----------------+------------+
| postid | meta_key       | meta_value |
+--------+----------------+------------+
|      1 | _regular_price |         20 |
|      1 | _price         |         20 |
+--------+----------------+------------+
2 rows in set (0.00 sec)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-13
    • 2021-02-19
    • 2020-04-21
    • 2014-04-13
    • 2015-08-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多