表插入 - 多个MySQL数据库

时间:2018-04-17 18:12:06

标签: mysql insert multiple-databases

我使用下面的Query尝试将Database2中table2的几行插入到Database1的table1中。这两个表都有不同的模式。

两个表都具有相同的结构。我确实检查了其他类似查询的帖子无济于事。使用以下基本方法:

 INSERT INTO    remoteDATABASE.remoteSCHEMA.remoteTABLE
  SELECT  * FROM    localTABLE

INSERT INTO 'eno*****.com'.asdf_stage.temp 
SELECT i.artf_id as  id,
    title as description,
    (case 
        when i.assigned_group = 'asdfk' then 'dfg'
        when i.assigned_group = 'dfgdg' then 'dgdk'
        when i.assigned_group = 'dfghdgf' then 'igo'
        when i.assigned_group = 'dfgh' then 'eMgem'
        when i.assigned_group = 'Edsfg' then 'esgd'
        end ) as area,
    "N/A" as disposition,
   "********" as flavor,
   "ipf" as link,
   "*************" as vendor,
    DATE(date_created) date,
  DATE(close_date) as completed
  FROM ******com.lkjhg_stage.issues  i
    where date(date_created) >'2018-04-01' AND status!='Rejected'

我收到语法错误,需要知道在两个地方提到数据库名称,架构名称和表名称的正确语法是什么。 此外,该表有大约10-15列,我是否需要指定所有列?

1 个答案:

答案 0 :(得分:0)

用单引号替换双引号。 DATE不是函数,保留字日期不是一个好的列名。

将您的表引用为SERVER.DATABASE.OWNER.OBJECT

INSERT INTO SERVER1.DATABASE1.OWNER1.OBJECT1
SELECT * FROM localTABLE

INSERT INTO SERVER1.DATABASE1.OWNER1.OBJECT1
SELECT i.artf_id as id,
    title as description,
    (case 
        when i.assigned_group = 'asdfk' then 'dfg'
        when i.assigned_group = 'dfgdg' then 'dgdk'
        when i.assigned_group = 'dfghdgf' then 'igo'
        when i.assigned_group = 'dfgh' then 'eMgem'
        when i.assigned_group = 'Edsfg' then 'esgd'
        end ) as area,
    'N/A' as disposition,
   '********' as flavor,
   'ipf' as link,
   '*************' as vendor,
    date_created,
    close_date as completed
FROM SERVER2.DATABASE2.OWNER2.OBJECT2  i
WHERE date_created > '2018-04-01' AND status != Rejected'