[英]SQL Change another row for id based off of value in a different row
I'm new to SQL and am trying to create a command that would do the following: 我是SQL的新手,正在尝试创建将执行以下操作的命令:
if _stripe_customer_id = cus_1 then for that post_ID, set _requires_manual_renewal = true and set _payment_method to BLANK and set _payment_method_title to BLANK. 如果_stripe_customer_id = cus_1,则对于该post_ID,将_requires_manual_renewal = true设置为,并将_payment_method设置为BLANK,并将_payment_method_title设置为BLANK。
I know what I need to but am not familiar with the commands to make it happen. 我知道我需要什么,但不熟悉实现它的命令。
Here is an example of my table (wp_postmeta) 这是我的表格(wp_postmeta)的示例
+-------+-------+-------------------------+------------+
|meta_id|post_id| meta_key | meta_value |
+-------+-------+-------------------------+------------+
| 1 | 11221 | _stripe_customer_id | cus_1 |
+-------+-------+-------------------------+------------+
| 2 | 11221 |_requires_manual_renewal | false |
+-------+-------+-------------------------+------------+
| 3 | 11221 | _payment_method | stripe |
+-------+-------+-------------------------+------------+
| 4 | 11221 | _payment_method_title | stripe |
+-------+-------+-------------------------+------------+
| 5 | 11223 | _stripe_customer_id | cus_1 |
+-------+-------+-------------------------+------------+
| 6 | 11223 |_requires_manual_renewal | false |
+-------+-------+-------------------------+------------+
| 7 | 11223 | _payment_method | stripe |
+-------+-------+-------------------------+------------+
| 8 | 11223 | _payment_method_title | stripe |
+-------+-------+-------------------------+------------+
| 9 | 11225 | _stripe_customer_id | |
+-------+-------+-------------------------+------------+
| 10 | 11225 |_requires_manual_renewal | true |
+-------+-------+-------------------------+------------+
| 11 | 11225 | _payment_method | |
+-------+-------+-------------------------+------------+
| 12 | 11225 | _payment_method_title | |
+-------+-------+-------------------------+------------+
Thanks for any help or direction that you provide! 感谢您提供的任何帮助或指示!
Assuming that for BLANK you mean null 假设对于BLANK,您的意思是null
update my_table a
inner join (
select post_id
from my_table
where meta_value = 'cus_1'
and meta_key ='_stripe_customer_id '
) t1 on t1.post_id = a.post_id
and meta_key in ('_payment_method', ' _payment_method_title')
set meta_value = NULL
Otherwise if you mean string empty for BLANK then 否则,如果您的意思是空白字符串为空,则
update my_table a
inner join (
select post_id
from my_table
where meta_value = 'cus_1'
and meta_key ='_stripe_customer_id '
) t1 on t1.post_id = a.post_id
and meta_key in ('_payment_method', ' _payment_method_title')
set meta_value = ''
You have to make 2 SQL statements 您必须制作2条SQL语句
UPDATE wp_postmeta
SET 'meta_value' = true
WHERE meta_key ='_requires_manual_renewal'
AND post_id IN
(
SELECT post_id
FROM wp_postmeta
WHERE meta_key ='_stripe_customer_id'
AND meta_value='cus_1'
)
;
UPDATE wp_postmeta
SET 'meta_value' = ''
WHERE meta_key IN ('_payment_method', '_payment_method_title')
AND post_id IN
(
SELECT post_id
FROM wp_postmeta
WHERE meta_key ='_stripe_customer_id'
AND meta_value='cus_1'
)
;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.