SQL根据另一行中的值更改id的另一行

时间:2019-03-02 17:01:03

标签: mysql

我是SQL的新手,正在尝试创建可以执行以下操作的命令:

如果_stripe_customer_id = cus_1,则对于该post_ID,将_requires_manual_renewal = true设置为,并将_payment_method设置为BLANK,并将_payment_method_title设置为BLANK。

我知道我需要什么,但不熟悉实现它的命令。

这是我的桌子(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  |            |
+-------+-------+-------------------------+------------+

感谢您提供的任何帮助或指导!

2 个答案:

答案 0 :(得分:0)

假设“空白”是空的

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 

否则,如果您要为空白表示字符串为空,则

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

答案 1 :(得分:0)

您必须制作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'
        )           
;