尝试在mysql中运行更新查询时出现语法错误

时间:2018-10-03 12:06:42

标签: mysql sql-update

在尝试执行以下给出的查询时,出现语法错误 我需要更新表civicrm_address中的列值,并将其从abc_abc_drupal_civi_4_17数据库移至abc_drupal数据库

为了实现它,我在FROM附近收到语法错误, 我得到的错误如下

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for
 the right syntax to use near 'FROM abc_abc_drupal_civi_4_17.civicrm_address,abc_drupal.civic' at line 3

我该如何解决?

UPDATE abc_drupal.civicrm_address 
SET abc_drupal.civicrm_address.state_province_id = abc_abc_drupal_civi_4_17.civicrm_address.state_province_id
FROM abc_abc_drupal_civi_4_17.civicrm_address,abc_drupal.civicrm_address
WHERE abc_drupal.civicrm_address.state_province_id IS NULL 
AND   abc_abc_drupal_civi_4_17.civicrm_address.state_province_id IS NOT NULL
AND   abc_abc_drupal_civi_4_17.civicrm_address.id = abc_drupal.civicrm_address.id  
AND   abc_abc_drupal_civi_4_17.civicrm_address.contact_id IS NOT NULL;

2 个答案:

答案 0 :(得分:1)

稍后您需要指定设置,如下所示:

   UPDATE TABLEA a 
   JOIN TABLEB b ON a.join_colA = b.join_colB  
   SET a.columnToUpdate = [something]



 UPDATE abc_drupal.civicrm_address 
    join abc_abc_drupal_civi_4_17.civicrm_address inner join abc_drupal.civicrm_address
    on abc_abc_drupal_civi_4_17.civicrm_address.id = abc_drupal.civicrm_address.id  
    SET abc_drupal.civicrm_address.state_province_id = abc_abc_drupal_civi_4_17.civicrm_address.state_province_id
where abc_drupal.civicrm_address.state_province_id IS NULL 
    AND  abc_abc_drupal_civi_4_17.civicrm_address.state_province_id IS NOT NULL
    AND  abc_abc_drupal_civi_4_17.civicrm_address.contact_id IS NOT NULL

答案 1 :(得分:1)

MySQL将JOIN放在UPDATE子句中,而不是通过单独的FROMFROM由SQL Server和Postgres使用)。

您所写的查询难以解读。我强烈建议您使用表别名,这样查询更容易编写和读取:

UPDATE abc_drupal.civicrm_address a JOIN
       abc_abc_drupal_civi_4_17.civicrm_address aa
       ON aa.id = a.id
    SET a.state_province_id = aa.state_province_id
WHERE a.state_province_id IS NULL 
      aa.state_province_id IS NOT NULL AND   
      aa.contact_id IS NOT NULL;