简体   繁体   English

根据另一行更新一行

[英]Update a row based on another row

I'm working on my wordpress database, I would like to update my table wp_postmeta and set all _order_tax field to be calculated from _order_total with the formula :我正在处理我的wordpress数据库,我想更新我的表wp_postmeta并将所有_order_tax字段设置为从_order_total使用公式计算:

_order_tax = 0.2 * _order_total _order_tax = 0.2 * _order_total

Here is a snapshot of my database (focusing two interesting rows) :这是我的数据库的快照(关注两个有趣的行): 在此处输入图片说明

I want to do this update for each row where meta_key = _order_total but I don't know how to write that the new value should be based on the _order_total value from the same post_id我想对meta_key = _order_total每一行进行此更新,但我不知道如何编写新值应基于来自同一post_id_order_total

For example, I would like to write (symbolic language not sql):例如,我想写(符号语言不是sql):

Where post_id is 1834 then update meta_value of row containing _order_tax in meta_key column with meta_value*0.2 of row containing _order_total in meta_key and same post_id as the line I want to update.其中post_id1834年则更新meta_value包含行的_order_taxmeta_keymeta_value*0.2包含行的_order_totalmeta_key和同post_id因为我想更新线路。

I hope my question is clear.我希望我的问题很清楚。

Thanks for helping谢谢你的帮助

You could use the UPDATE ... JOIN ... SET ... WHERE ... syntax as follows:您可以使用UPDATE ... JOIN ... SET ... WHERE ...语法如下:

UPDATE wp_postmeta w1
INNER JOIN wp_postmeta w2 
    ON  w2.post_id = w1.post_id 
    AND w2.meta_key = '_order_total' 
SET w1.meta_value = w2.meta_value * 0.2
WHERE w1.meta_key = '_order_tax'

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

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