删除mysql中2个表之间的重复项

时间:2016-07-05 10:12:20

标签: mysql

table1

   id     mcs     name     search_id
    1     14      name1    70
    2     14      name2    70
    3     14      name3    70

table2

  id    mcs     name     search_id
   1    14      name1    70
   2    14      name2    70
   3    14      name4    70
   4    14      name5    70
   5    14      name6    70

我想要的是从table2中选择行而不是table1中的行,它们具有相同的mcs和search_id ....我的尝试是

 create temporary table find_duplicates SELECT * FROM table2 T1 WHERE
            T1.mcs = T2.mcs AND T1.search_id = T2.search_id AND  T1.name IN ( SELECT T2.name FROM table2 T2) ;

但它给了我重复的列名为id ... 即使我试过这个

     create temporary table find_duplicates SELECT )mcs,name,search_id) FROM table2 T1 WHERE
            T1.mcs = T2.mcs AND T1.search_id = T2.search_id AND  T1.name IN ( SELECT T2.name FROM table2 T2) ;

它给我的列含糊不清

任何帮助???

2 个答案:

答案 0 :(得分:0)

/*
create table table1 (id  int,   mcs int  ,  name  varchar(10),     search_id int);
insert into table1 values
(    1,     14,      'name1',    70),
(    2,     14,      'name2',    70),
(    3,     14,      'name3',    70);

create table table2 (id  int,   mcs int  ,  name  varchar(10),     search_id int);
insert into table2 values
 (  1,    14,      'name1' ,   70),
 (  2,    14,      'name2' ,   70),
 (  3,    14,      'name4' ,   70),
 (  4,    14,      'name5' ,   70),
 (  5,    14,      'name6' ,   70);
 */

 create temporary table find_duplicates
 select t2.* 
 from   table2 t2
 left outer join        table1 t1 on t1.mcs = t2.mcs and t1.name = t2.name and t1.search_id = t2.search_id
 where  t1.id is null;

答案 1 :(得分:0)

create temporary table find_duplicates
select distinct(t2.id) from table2 t2,table1 t1 where t1.mcs = t2.mcs 
and t1.search_id = t2.search_id and not find_in_set 
(t2.name,(select group_concat(name) from table1))