从不同表

时间:2016-12-06 17:15:45

标签: mysql sql opencart

由于一些不可预见的情况,我所拥有的客户的Open Cart表已经将address_id重置为0以用于所有记录,这现在会导致问题。

我确实有sql的备份,但是我只在INSERT表单中使用它,但由于自上次备份以来发生了一些变化,我更喜欢它是UPDATE语句。

所以,而不是:

INSERT INTO oc49_customer (customer_id,firstname,surname,address_id etc.) VALUES ('1','Joe','Bloggs','1' ...), ('2','Jane','Doe','2'...)

我更愿意:

UPDATE oc49_customer SET address_id = 1 WHERE customer_id = 1

有没有办法轻松实现这一点,因为现在我有两个表 - 现有的客户表和一个customer_temp表,它是具有正确地址ID的备份?

2 个答案:

答案 0 :(得分:2)

是的,使用子查询

Update oc49_customer
  set address_id =
      (Select address_id from oc49_customer_old
       where customer_id = oc49_customer.customer_id)
From oc49_customer

也可以在没有From子句的情况下尝试:

Update oc49_customer
  set address_id =
      (Select address_id from oc49_customer_old
       where customer_id = oc49_customer.customer_id)

答案 1 :(得分:0)

UPDATE oc49_customer
SET address_id = (SELECT address_id
  FROM oc49_customer_old
  WHERE (oc49_customer_id = oc49_customer_old_id)
WHERE EXISTS ( oc49_customer_id = oc49_customer_old_id )