在同一服务器上的两个数据库之间使用2个内部联接更新查询

时间:2013-11-20 23:58:15

标签: mysql sql sql-update inner-join

我正在尝试在执行两个内部联接之后更新一个数据库中的表中的列,其中一个是来自同一服务器上另一个数据库的表。我试图在这里遵循这个解决方案: Update Query with INNER JOIN between tables in 2 different databases on 1 server

这对我不起作用。它告诉我:你的语法在s_u

附近有错误

这是我的尝试(更新:我删除了代码卫生。所以这是我现在正在运行的确切代码):

UPDATE s_u
  SET s_u.bill_address_id=spree_billing.id
  FROM spree_users AS s_u
  INNER JOIN magento.customer_entity_int AS default_billing
    ON default_billing.entity_id=s_u.magento_id
    AND default_billing.attribute_id=14
  INNER JOIN spree_addresses AS spree_billing
    ON spree_billing.magento_address_id=default_billing.value;

2 个答案:

答案 0 :(得分:1)

您引用了一个名为spree_billing的内容,它不会出现在您的语句中的任何位置。你需要解决它。问题可能在这里:

UPDATE s_u
  SET s_u.bill_address_id=billing.id  -- modify to billing, as it is what third_table's alias is called
  FROM some_table AS s_u
  INNER JOIN magento.another_table AS default_billing
    ON default_billing.entity_id=s_u.magento_id
    AND default_billing.attribute_id=14
  INNER JOIN third_table AS billing
    ON billing.magento_address_id=default_billing.value;

答案 1 :(得分:0)

请你试试这个:

UPDATE spree_users AS s_u
 INNER JOIN magento.customer_entity_int AS default_billing
   ON default_billing.entity_id=s_u.magento_id
   AND default_billing.attribute_id=14
 INNER JOIN spree_addresses AS spree_billing
   ON spree_billing.magento_address_id=default_billing.value
SET s_u.bill_address_id=spree_billing.id;

我没有尝试过您的表格,但是在我的样本测试中进行测试

答案中的答案是SQL-Server。你应该看看这个链接。 UPDATE multiple tables in MySQL using LEFT JOIN

更新

因此,语法错误被删除但需要很长时间。我想检查有多少记录匹配JOIN或连接有效执行。请你发布以下查询的输出?

SELECT COUNT(*)
FROM spree_users AS s_u
 INNER JOIN magento.customer_entity_int AS default_billing
   ON default_billing.entity_id=s_u.magento_id
   AND default_billing.attribute_id=14
 INNER JOIN spree_addresses AS spree_billing
   ON spree_billing.magento_address_id=default_billing.value