【问题标题】:Update MySQL database column where metadata is copied from one row to another更新 MySQL 数据库列,其中元数据从一行复制到另一行
【发布时间】:2019-01-10 20:54:19
【问题描述】:

我遇到了一种情况,我需要更新 MySQL 数据库列中的数据,我将数据从一行相关数据的列复制到同一列但在不同的行中。 这是一个名为 "wp_woocommerce_order_itemmeta". 的 Wordpress/WooCommerce 数据库表,我希望能够复制 meta_value where meta_key=_product_id to meta_value column where meta_key=_variation_id whereever the order_item_id numbers match 的值。 因此,在屏幕截图中,您会看到所有 order_item_id 值都相同(它们都是 8) - 我希望将 meta_value 列中的 172 复制到 meta_value column where meta_key=_variation_id

https://snag.gy/DMrfXu.jpg

我希望能够通过使用 _product_id 值来更新这些,因为我有大约 180 种产品,而且我可以很快地做到这一点。但是,我不能通过包含 order_item_id 数字来做到这一点,因为我有数万个订单。

在从 OpenCart 迁移后,变体 ID 编号全部为 0。我希望有一种方法可以运行这样的查询。

【问题讨论】:

  • 您可以尝试类似:SET column=(Select value from table where row=val Limit 1)
  • 谢谢,但我不知道如何才能在这种情况下构建它。

标签: mysql sql wordpress woocommerce orders


【解决方案1】:

我找到了自己问题的答案。 这对我有用:

UPDATE wp_woocommerce_order_itemmeta as wp1, wp_woocommerce_order_itemmeta as wp2 
SET wp2.meta_value = replace(wp2.meta_value,'0','172') 
WHERE wp1.order_item_id = wp2.order_item_id AND wp1.meta_key = '_product_id' 
AND wp2.meta_key = '_variation_id' AND wp1.meta_value='172' AND wp2.meta_value='0';

我希望这可能对将来的某人有所帮助。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-07-20
  • 2018-10-31
  • 2016-04-16
  • 2011-04-25
  • 2018-02-24
  • 2011-11-07
相关资源
最近更新 更多