日期:2014-05-18 浏览次数:20497 次
table1: id,value 1,1111 2,2222 3,4444 4,1111 5,2222 table2: id,table1Id,Table1Value(唯一约束)
INSERT table2 ( table1Id , Table1Value ) SELECT table1id , value FROM table1 GROUP BY table1id , value
------解决方案--------------------
insert into table2(table1Id,Table1Value) select min(id) as id,value from table1 group by value
------解决方案--------------------
table2中没数据的话2、3L的方法可以解决,有数据的话还需要在插入前再比较一次
------解决方案--------------------
2楼的可以,用分组,下面的我用的是rownumber分组,思想差不多。
insert into table2(table1id,table1value) select id,value from ( select id,value,ROW_NUMBER() over (partition by value order by id) as rownum from table1 ) as t where rownum<2