将行从一个表复制到另一个表,忽略重复

时间:2012-07-31 11:25:16

标签: mysql

我试图仅使用2个coluom将行从表复制到另一个,因为两个表的架构不相同, 我收到此错误

操作数应包含1列

我的陈述中有任何提示错误吗?

 Insert table1 ( screenname,list_id )
  Select screenname,list_id
 From table2 As T1
 Where Not Exists    (
                Select 1
                From table1 As T2
                Where 
               (T2.screenname = T1.screenname,T2.list_id = T1.list_id)
                )

5 个答案:

答案 0 :(得分:2)

尝试将条件从(T2.screenname = T1.screenname,T2.list_id = T1.list_id)更改为(T2.screenname = T1.screenname AND T2.list_id = T1.list_id)

(注意AND关键字而不是逗号)

答案 1 :(得分:1)

您是否尝试过INSERT INTO ... ON DUPLICATE KEY语法?

请参阅MySQL手册here

答案 2 :(得分:1)

您可以在screenname和list_id列的table1中创建唯一索引 然后使用以下语句

  

将ignore插入table1(screenname,list_id)     选择screenname,list_id    从table2作为T1

答案 3 :(得分:0)

也可以尝试此查询 -

INSERT INTO table1 (screenname, list_id)
  SELECT screenname, list_id FROM table2 t2
    LEFT JOIN table1 t1
      ON t1.screenname = t2.screenname AND t1.list_id = t2.list_id
  WHERE
    t1.screenname IS NULL AND t1.list_id IS NULL;

答案 4 :(得分:0)

使用简单的INSERT IGNORE

INSERT table1 (screenname, list_id) SELECT screenname, list_id FROM table2