简体   繁体   English

更新MySQL数据库列,其中元数据从一行复制到另一行

[英]Update MySQL database column where metadata is copied from one row to another

I've been presented with a situation where I need to update data in a MySQL database column where I copy the data from the column of one row of related data to the same column but in a different row. 我遇到的情况是,我需要更新MySQL数据库列中的数据,在该列中,我将数据从一行相关数据的列复制到同一列但在另一行。 It is a Wordpress/WooCommerce database table called "wp_woocommerce_order_itemmeta". 这是一个名为"wp_woocommerce_order_itemmeta".的Wordpress / WooCommerce数据库表"wp_woocommerce_order_itemmeta". I want to be able to copy the value of the meta_value where meta_key=_product_id to meta_value column where meta_key=_variation_id whereever the order_item_id numbers match . 我希望能够将meta_value where meta_key=_product_id to meta_value column where meta_key=_variation_id whereever the order_item_id numbers matchmeta_value where meta_key=_product_id to meta_value column where meta_key=_variation_id whereever the order_item_id numbers match的值复制到meta_value where meta_key=_product_id to meta_value column where meta_key=_variation_id whereever the order_item_id numbers match So, in the screenshot you see that all the order_item_id values are all the same (they are all 8) - I want the 172 in the meta_value column to be copied to the meta_value column where meta_key=_variation_id 因此,在屏幕截图中,您会看到所有order_item_id值都相同(它们全为8)-我希望将meta_value列中的172复制到meta_value column where meta_key=_variation_id

https://snag.gy/DMrfXu.jpg https://snag.gy/DMrfXu.jpg

I want to be able to update these by using the _product_id values because I have around 180 products and I can do this rather quickly. 我希望能够通过使用_product_id值来更新这些值,因为我大约有180种产品,而且可以很快地做到这一点。 I can not, however, do this by including the order_item_id numbers because I have tens of thousands of orders. 但是,我无法通过包含order_item_id数字来完成此操作,因为我有成千上万的订单。

The variation ID numbers all came through with the value of 0 after a migration from OpenCart. 从OpenCart迁移后,所有版本ID都带有0值。 I'm hoping there is a way to run a query like this. 我希望有一种方法可以运行这样的查询。

I figured out the answer to my own question. 我想出了自己的问题的答案。 This is what worked for me: 这对我有用:

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';

I hope this might help someone in the future. 我希望这可以在将来对某人有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM